What is the dollar sign used for before a variable?

Posted on

Question :

I would like to know the function of the dollar sign, and also if it is necessary that the variable be glued (without any space) in the equal sign (=) and the value of the variable (GABRIEL)


echo $NOME


Answer :

Serves to access values stored within variables. When you use the $ prefix you are wanting to access this value. Here’s a simple example.


NAME="Zara Ali"
echo $NAME

I declared a NAME variable and stored the value “Zara Ali”. To ACCESS this value “Zara Ali”, I need the prefix $


In Unix, you can choose the shell you want to use.
Example: bash dash zsh sh ksh csh … Each has its syntactic and semantic definition.

In various Linux and Mac distributions, the default shell is bash: let’s assume we’re talking Bash . Here are some conventions:


  • By default the identifiers are commands, files, folders, arguments: there is a need to “tag” the variables in some way.

Definition of variables / assignment

  • id=exp assigning a value to a variable
  • id = exp executes the “id” command passing it as arguments “=” and “exp”
  • id= date assigns empty value to id and executes date

About $ depending on the context, can mean a lot of different things:

  • $id id value
  • echo "o meu username é $USER" the value of the variable is expanded inside quoted strings (prints “my username is jj”)
  • $(comand) results in command stdout
  • $((3 + 4 + $a )) results in the arithmetic calculation contained therein
  • a=(v0 v1 v2 v3 v4 v5) ; echo ${a[2]} gives the second value of an array (v2)

A small example:

$ A=ano
$ echo "o $A passado foi $(( $(date +%Y) - 1))"
o ano passado foi 2016

Finally, remember that bash has:

  • variables, arrays, dictionaries
  • control structures (if, while, switch, for, …)
  • functions (including recursive)

Leave a Reply

Your email address will not be published. Required fields are marked *