mrdaliri
mrdaliri

Reputation: 7328

What's the difference between "$(variable)" and "$(VARIABLE)"

What's the difference? And why these are working:

echo $LOGNAME       #prints username
echo "$(logname)"   #prints username

but this isn't:

echo "$(LOGNAME)"   #prints command not found.

Upvotes: 2

Views: 91

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74596

$LOGNAME is a variable. logname is a command. When you do

echo $LOGNAME

you are echoing the variable, whereas when you do

echo "$(logname)"

you are echoing the result of executing the command. It happens to be the case that the output is the same.

If you do env | grep LOGNAME, you will see that $LOGNAME is an environment variable and if you do which logname you will see the path to the executable. However, if you do which LOGNAME, you will see that there is no output. echo $? shows that the exit status of the command is 1, which means that no executable could be found.

Coincidentally, you can do the same thing with $PWD and pwd.

Upvotes: 4

Jdamian
Jdamian

Reputation: 3115

logname is a command.
LOGNAME is a variable.

$(logname) works because logname command exists.
$(LOGNAME) tries to run the command LOGNAME which does not exist.

Read the following useful guide

Upvotes: 4

Related Questions