6

What is the difference between using an environment variable, like PATH, as $PATH or ${PATH}?

3 Answers 3

18

There's no difference in most cases. The only times it matters is if you want to include trailing text after the expansion. For example, suppose your PATH contained the string FOO (not actually a valid path, but this is for an example), and you wanted to form the string FOOBAR. If you did

$PATHBAR

You would get the expansion of the variable named PATHBAR, which is probably not what you wanted. If you did

$PATH BAR

You would get a space between FOO and BAR, also not what you wanted. The solution is to use braces:

${PATH}BAR

This gives you FOOBAR.

Sign up to request clarification or add additional context in comments.

Comments

5

PATH is the name of the environment variable, $PATH and ${PATH} are methods of accessing them. The form ${PATH} is used to allow constructs like echo ${PATH}b which would fail using $PATHb. Also, bash allows lots of parameter replacement stuff which the man page will gladly tell you more about.

2 Comments

what does the b after ${PATH} mean?
It'll put a literal "b" after PATH instead of accessing an environment variable named PATHb. Try it yourself.
0

In your case there is no difference, but - take the situation: You have two variables:

$FOO = "YA"
$YADA = "bar"

then ${$FOODA} will give you nothing while ${${FOO}DA} will give you "bar"

2 Comments

'$FOO = "YA"' is incorrect -- it doesn't do what you think it does. You need to do 'FOO-="YA"'
No, ${${FOO}DA} will give you a syntax error, but eval '$'{${FOO}DA} will give the contents of $YADA...if you assign to FOO correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.