Home > other >  Can long $PATH be split across many lines?
Can long $PATH be split across many lines?

Time:10-19

Right now, my path declaration looks like this: export PATH=/a/b/c:/d/e/f:/g/h/i:/j/k/l and so on, for a length of over 1,000 characters, currently in one very long line. What I would like to do, for ease of maintenance, is declare it in multiple lines, for example: NOTE: text in angle brackets <> is not actual syntax.

export PATH=<some way of indicating that we continue on the next line>
/a/b/c:<newline>
/d/e/f:<newline>
/g/h/i:<newline>
/j/k/l:<newline>

and so on.

As I formulated this question, it came to me that I could achieve something similar by coding:

export PATH=/a/b/c
export PATH=$PATH:/d/e/f
export PATH=$PATH:/g/h/i
export PATH=$PATH:/j/k/l

and so on.

But is there a way to do it in a single export command?

CodePudding user response:

You can use a backslashed, followed immediately by a new line, to indicate line continuation, but I would not do it. This just invites errors to creep in which are not easily to see on the first glance.

Instead of this, I would use the array path instead of the string PATH, and write

path=(
  /a/b/c
  /d/e/f
  /g/h
)

Note that PATH and path are synchronized: If you change one, the other one is changed along.

UPDATE: While path=(....) is the equivalent to your original request, a more realistic use will probably be path =(.....), because you want in general the existing path not be destroyed.

As for the export aspect, PATH will, in generally, be already exported, so you won't do this explicitly. If you do want to do it - for whatever reason -, you would just add a single

 export PATH

afterwards, since you can't export arrays.

  • Related