Multiple path environment variable setup lines with bash
Solution 1
You can do:
export PATH="A"
export PATH="$PATH:B"
export PATH="$PATH:C"
Each subsequent line appends onto the previously defined path. This is generally a good habit, as it avoids trashing the existing path. If you want the new component to take precedence, swap the order:
export PATH="A"
export PATH="B:$PATH"
export PATH="C:$PATH"
Alternatively, you might be able to do:
export PATH=A:\
B:\
C
where \
marks a line continuation. Haven't tested this method.
Solution 2
You can extend lines in bash using a backslash at the end of a line like this:
export PATH=/path/A:\
/path/B:\
/path/C
Please note that the absence of white space is important here.
Solution 3
Another approach:
export PATH=$(tr -d $'\n ' <<< "
/path/A:
/path/B:
/path/C")
Has the added benefit of not messing up your indent levels.
Related videos on Youtube
prosseek
A software engineer/programmer/researcher/professor who loves everything about software building. Programming Language: C/C++, D, Java/Groovy/Scala, C#, Objective-C, Python, Ruby, Lisp, Prolog, SQL, Smalltalk, Haskell, F#, OCaml, Erlang/Elixir, Forth, Rebol/Red Programming Tools and environments: Emacs, Eclipse, TextMate, JVM, .NET Programming Methodology: Refactoring, Design Patterns, Agile, eXtreme Computer Science: Algorithm, Compiler, Artificial Intelligence
Updated on September 18, 2022Comments
-
prosseek over 1 year
I have very long
export PATH=A:B:C ...
. Can I make a multiple lines to have more organized one as follows?export PATH = A: B: C:
-
Gilles 'SO- stop being evil' about 13 yearsNote that
export
is a built-in command, not a keyword nor a syntactic assignment. So if you havePATH
elements containing whitespace (or glob characters), you do need double quotes aroundexport PATH="$PATH:B"
. You could also writePATH=$PATH:B
and so on; you only need toexport
a variable once, not every time it changes (except in some very old Bourne shells), and you don't need the double quotes in an assignment. -
Andrew G. almost 11 yearsAlso
PATH+=:B
works for string concatenation.