As far as I understand, the usual way to add a path to the PATH environment variable is by concatenating paths separated by the : character.
For example, if I want to add three paths to it /my/path/1, /my/path/2 and /my/path/3, I would have to do it as follows:
PATH=$PATH:/my/path/1:/my/path/2:/my/path/3
which is not easy to read.
Is there a way to define or add paths to the PATH variable using a multi-line syntax? Perhaps using arrays? I am looking for something like this:
PATH = $PATH /my/path/1 /my/path/2 /my/path/3
In case there are interesting solutions that are shell specific, I am looking for a solution in zsh.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Not an interesting solution at all, but very portable:
PATH=${PATH}:/my/path/1
PATH=${PATH}:/my/path/2
PATH=${PATH}:/my/path/3
Method 2
In zsh, $path is an array:
path=(
$path
~/.local/bin
~/.gem/ruby/2.0.0/bin
)
Note: both path is lowercase.
Method 3
I do not know if it works in zsh, but it works in bash:
PATH=$(paste -d ":" -s << EOF $PATH /my/path/1 /my/path/2 /my/path/3 EOF )
Edit and even shorter:
PATH=`paste -d ":" -s << EOF $PATH /my/path/1 /my/path/2 /my/path/3 EOF`
And without spawning a process:
new_path=(
"$PATH"
/my/path/1
/my/path/2
/my/path/3)
OLD_IFS="$IFS"
export IFS=":"
PATH="${new_path[*]}"
export IFS="$OLD_IFS"
The double quotes are important arround $PATH, $IFS, ${new_path[*]} and $OLD_IFS to keep spaces in the variables and avoid shell interpretation of IFS.
Update2 with comments and empty line management using sed:
PATH=`sed -e '/^#/'d -e '/^$/'d << EOF | paste -d ":" -s $PATH /my/path/1 # This is a comment. /my/path/2 /my/path/3 EOF`
The comment character must be the 1st char on the line, and empty lines should be completely empty. To manage spaces and tabs before comment and on empty lines, use sed -e '/^[ t]*#/'d -e '/^[ t]*$/'d instead (tabs to be tested as it might be specific to the sed implementation).
Method 4
In zsh, if you’re adding directories at the end of the path:
path+=/my/path/1
path+=/my/path/2
path+=(/path/to/app/{i386,share}/bin)
Portably: How to correctly add a path to PATH?
You can use glob qualifiers to exclude entries that are not existing directories or symbolic links to such. This may or may not be desirable, depending on whether you expect the directories to possibly be added later during the session (e.g. if they’re on remote filesystems). You can’t do that with the path+=/my/path syntax because the right-hand side is in a string context and so doesn’t undergo globbing; you can do it with path+=(/my/path) since each array element is expanded in a list context.
path+=(/path/to/app/{i386,share}/bin(-/N))
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0