Ubuntu – Using ‘$’ in shell

command line

I am new to ubuntu, and tried to use OpenFOAM Installation worked OK; after that I copy-pasted the very first example, worked also OK. but I cannot understand how.
There are funny commands like:

mkdir -p $FOAM_RUN 

This creates the folder structure /home/username/OpenFOAM/username-2.3.1/run

Or

cp -r $FOAM_TUTORIALS $FOAM_RUN 

This copies the tutorials in the above mentioned folder

What does the sign $ do?

These commands work when copy-pasted, but I want understanding them and being able to use them later on.

Best Answer

The name of a variable is a placeholder for its value, the data it holds. Referencing (retrieving) its value is called variable substitution. The $ sign helps us to get the value.

Let us carefully distinguish between the name of a variable and its value. If variable1 is the name of a variable, then $variable1 is a reference to its value, the data item it contains, for example:

We define variable1 with a value 23:

$ variable1 = 23

To print the name of the variable (echo is just like a print function in the terminal):

$ echo variable1
variable1

To print the actual value saved in the variable

$ echo $variable1
23
Related Question