0

This question already has an answer here:

There are few different formats I have seen so far:

  1. With quotes and brackets:

    PATH="/usr/local/bin:${PATH}"
    
  2. With quotes only:

    PATH="/usr/local/bin:$PATH"
    
  3. None:

    PATH=/usr/local/bin:$PATH
    

Export all the same:

export PATH

Which is the correct/preferred way?

marked as duplicate by jasonwryan, muru, jimmij, Anthon, Michael Homer Feb 8 '15 at 0:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

This is about shell variables in general, not just PATH.

Here are some examples of why "" and {} can reduce errors.

Putting it in quotes is safer: if you do a=hello world then you will not get what you expect, but for a="hello world" you will.

Using {} is also safer: doing h="hello"; echo "$hword" will not work, but h="hello"; echo "${h}word" will.

1

They all are effective. One isn't drastically better than the other, but I prefer:

export PATH=$PATH:/usr/local/bin

It takes care of the export and the setting of the path in one line. I also tend to not put the new path before the existing $PATH, but there are cases when that might be necessary to load newer self-compiled libraries before older system ones.


If you are trying to export variables then yes, you want to quote them, for instance:

export myservers="server1 server2 server3"

Now when you echo $myservers you will see:

[user]# echo $myservers
server1 server2 server3

But since this question relates to $PATH and not shell variables, then my original post still stands since there will never be a time where you are printing 'hello world' into your system path.

[user]# echo $PATH  ## Something you shouldn't be doing
/usr/local/bin:HELLO WORLD/sbin:/bin:/usr/sbin:WHY AM I DOING THIS?/usr/bin:/root/bin
  • Don't forget to quote your variables. – muru Feb 7 '15 at 20:14
  • Note this example will produce a different result that the examples in the question: The order does matter. – ctrl-alt-delor Feb 7 '15 at 22:47
  • @richard Note, I do mention that in my paragraph. – devnull Feb 7 '15 at 23:11

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