1

I am just wondering why do we need to use this command

export Path=$Path:dir

why does the command not look like

export $Path=$Path:dir/dir'
  • because that is how bash works. $PATH is expanded by the shell to the value it contains, that is why you cannot use $ in assignments. – Fredrik Pihl Jan 25 '15 at 20:59
  • Case is important. You need $PATH not $Path – Basile Starynkevitch Jan 25 '15 at 21:46
3

The dollar sign $ is used to access a variable, not set it. An easy way to see this is to try and print a variable:

test="Hello"
echo test
# prints 'test'
echo $test
# prints 'Hello'
$test="Bye"
# Not allowed
| improve this answer | |
7

If you do this:

VAR=value

bash sets VAR to "value", creating the variable, if it didn't exist.

If you write:

$VAR=value

then bash executes this replacing $VAR by its current value. Say, $VAR had the value x, this would produce:

x=var

as the command to be executed. That's why it says "command not found"

However, bash can be told to reinterpret this via eval:

VAR2=x
VAR=VAR2
echo $VAR2
x

eval $VAR=y
echo $VAR2
y
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy