0

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?

Stickers
  • 123

2 Answers2

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
devnull
  • 5,431