16

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.

12

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).

  • I tested the last snippet in zsh and it works. I also like that it's a bit cleaner (one path per line) than the other solutions. Can the solution accomodate commented lines (paths)? How about empty lines? – Amelio Vazquez-Reina Feb 8 '12 at 16:37
  • Update with comments and empty line management. – jfg956 Feb 8 '12 at 17:16
  • And with sed, if you want to play, you can avoid the paste process with a more complicated script. – jfg956 Feb 8 '12 at 17:18
  • Is it OK to use indentation for the paths in the paste solution? – void.pointer Jul 2 '14 at 16:42
20

Not an interesting solution at all, but very portable:

PATH=${PATH}:/my/path/1
PATH=${PATH}:/my/path/2
PATH=${PATH}:/my/path/3
  • 1
    This is typical of what I've seen over the years. One can easily reorder the paths or even change an element to be PATH=/my/path/1:${PATH} to force prepending. – bsd Feb 8 '12 at 15:41
  • 1
    You could also use += to avoid having to completely rebuild the string. – Chris Down Feb 8 '12 at 17:33
  • And you don't even need to use the braces. Colons can't be used in environment variable names so there's no ambiguity to resolve. – Alexios Feb 8 '12 at 18:33
  • +1 for not being interesting. Interesting can be good sometimes, but this isn't one of those times! (Especially since the OP explicitly mentioned "not easy to read" as a problem.) – ruakh Feb 8 '12 at 19:54
  • @ChrisDown Is there a syntax which prepends without rebuilding the string? – John P Jan 22 '17 at 15:28
12

In zsh, $path is an array:

path=(
    $path
    ~/.local/bin
    ~/.gem/ruby/2.0.0/bin
)

Note: both path is lowercase.

  • That depends on the shell, not all handle this. – vonbrand Apr 25 '13 at 10:00
  • 1
    @vonbrand: Yes, But works in zsh. I think it worth to mention. – Kabie Apr 25 '13 at 10:21
  • 3
    +1 (This question is tagged zsh) – Johnsyweb Jun 12 '13 at 10:06
  • Since the OP specifically asked about zsh, this is the best option – Daniel Serodio Sep 27 '16 at 18:43
  • 1
    for anyone interested in learning more about the $path vs. $PATH distinction check out this post – mbigras Oct 24 '16 at 7:45
7

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))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.