How to permanently add to PATH?

Note that I've read How to add permanently to PATH.

Each time I start a terminal session I need to run source ~/.zsh_profile, but why hasn't hc, an executable in ~/hc, been permanently added to my PATH when the directory is in two profiles? Yes, I realise it's best to have it in just one place otherwise it will be added twice.

 10:55:38  jr@fm  ~  ⬡ v8.16.0 
$ cat ~/.zsh_profile
export PATH="$HOME/hc:$PATH" 
export PATH="$HOME/holochain:$PATH"
export PATH="$HOME/.cargo/bin:$PATH"
source ~/.zshrc

 10:55:50  jr@fm  ~  ⬡ v8.16.0 
$ cat .profile
export EDITOR=/usr/bin/nano

export PATH="$HOME/.cargo/bin:$PATH"
export PATH="$HOME/hc:$PATH" 
export PATH="$HOME/holochain:$PATH"
export PATH="$HOME/.cargo/bin:$PATH"


 15:26:08  jr@fm  ~  ⬡ v8.16.0 
$  hc --version
zsh: command not found: hc

Do you have a .zshrc file in your home directory? If so, add...

source ~/.zsh_profile

... to it.

Then log out of the session by typing...

exit

... at the prompt or hitting Ctrl+D, and then reopen the Konsole window. It should now work. :slight_smile:

Maybe because your default shell is not zsh?

If you want zsh to be your default shell run this in a terminal ($USER is environment for current logged in user)

sudo chsh -s /bin/zsh $USER

What ever the reason, this is what I have in my ~/.profile and ~/.xprofile

You need to adjust to your case but I think you get the idea and beware of duplicates.

if [ -d "$HOME/bin" ]; then
    PATH=$HOME/bin:$PATH
fi
2 Likes

OK there were a couple of other things that I had to do but it's sorted now. In the interim there was an infinite recursion, I think because I also had source ~/.zshrc in ~/.zsh_profile, and was exporting the hc and holochain folders to the PATH in both files. But it's fixed now! When I open a terminal the hc command is recognized.

@linux-aarhus, zsh is my default profile.

There are a couple of duplicates that I still need to remove:

$ echo $PATH 
/home/jr/.nvm/versions/node/v8.16.0/bin:/home/jr/bin:/usr/local/bin:/home/jr/.cargo/bin:/home/jr/holochain:/home/jr/hc:/home/jr/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/jr/.yarn/bin

I just realized why I have my home path twice in X - it is because I have it in both profile and xprofile.

So .profile should be enough as this will propagate to X when X is launched.

1 Like

I would make that...

if [ -d "$HOME/bin" ]; then
    export PATH=$HOME/bin:$PATH
fi

... just so as to do it right the first time around. :stuck_out_tongue:

And strictly speaking, you don't even need to test for the existence of ~/bin, because it won't do any harm to add it to your $PATH anyway if it doesn't exist. :slight_smile:

I'd rather not have duplicates in my PATH.

Thanks @linux-aarhus, I made adjustments again.

I also added functions from https://stackoverflow.com/a/47159781/7438857 to /etc/profile.

Restarting my terminal now I don't have duplicates in my PATH.

The pathremove function doesn't work, however, including after restarting the terminal and logging out.

$ pathremove
zsh: command not found: pathremove

$ cat /etc/profile
# /etc/profile
# Functions to help us manage paths.  Second argument is the name of the
# path variable to be modified (default: PATH)
pathremove () {
        local IFS=':'
        local NEWPATH
        local DIR
        local PATHVARIABLE=${2:-PATH}
        for DIR in ${!PATHVARIABLE} ; do
                if [ "$DIR" != "$1" ] ; then
                  NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
                fi
        done
        export $PATHVARIABLE="$NEWPATH"
}

pathprepend () {
        pathremove $1 $2
        local PATHVARIABLE=${2:-PATH}
        export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"
}

pathappend () {
        pathremove $1 $2
        local PATHVARIABLE=${2:-PATH}
        export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1"
}

# Set our umask
umask 022

# Append our default paths
appendpath () {
    case ":$PATH:" in
        *:"$1":*)
            ;;
        *)
            PATH="${PATH:+$PATH:}$1"
    esac
}

appendpath '/usr/local/sbin'
appendpath '/usr/local/bin'
appendpath '/usr/bin'
unset appendpath

export PATH

# Load profiles from /etc/profile.d
if test -d /etc/profile.d/; then
        for profile in /etc/profile.d/*.sh; do
                test -r "$profile" && . "$profile"
        done
        unset profile
fi

# Source global bash config
if test "$PS1" && test "$BASH" && test -z ${POSIXLY_CORRECT+x} && test -r /etc/bash.bashrc; then
        . /etc/bash.bashrc
fi

# Termcap is outdated, old, and crusty, kill it.
unset TERMCAP

# Man is much better than us at figuring this out
unset MANPATH

 16:19:39  jr@fm  ~  ⬡ v8.16.0 
16:20:59  ✘  jr@fm  ~  ⬡ v8.16.0 
$ sh
sh-5.0$ pathremove
sh: pathremove: command not found
sh-5.0$ bash
[jr@fm ~]$ pathremove
bash: pathremove: command not found
[jr@fm ~]$ zsh

 16:23:01  jr@fm  ~  ⬡ v8.16.0 

Anyway, it doesn't matter at the moment as there are no duplicates in my PATH when I open a new terminal.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

Forum kindly sponsored by