One liner to set environment variable if doesn't exist, else append
A little improvement on Michael Burr's answer. This works with set -u
(set -o nounset
) as well:
PATH=${PATH:+$PATH:}/path/to/bin
PATH=${PATH}${PATH:+:}/path/to/bin
${PATH}
evaluates to nothing ifPATH
is not set/empty, otherwise it evaluates to the current path${PATH:+:}
evaluates to nothing if PATH is not set, otherwise it evaluates to ":"