Special Variables in Linux Shell: $0, $?, $$, $#, $@ with Examples
An important characteristic of shell scripts is the possibility of confirming the successful execution of a given command.
Special return variables
It is called a return variable.
The return variable is filled every time a command is executed. If the returned value is 0, the command was successfully executed.
If the value is different from 0, some problem was encountered.
If used in scripts, its value must be checked immediately after executing the desired command.
| Variable | Purpose |
| $# | Returns the number of arguments the program received. |
| $_ | The underscore contains the absolute file name of the shell or script being executed as passed in the argument list. |
| $? | Return variable for the termination of a program's execution. |
| $$ | Provides the PID of the Shell process. |
| $! | Provides the PID of the last program running in the background. |
| $0 | Returns the name of the executed program. |
| $n | Returns argument number n of the program. |
| $* $@ | Returns all arguments provided in the program's execution. |
See the example script that uses these variables:
#!/bin/bashecho "Meu nome é: $0"echo "Eu tenho $# argumentos passados na minha execução"echo "Este é o resultado da última execução: $_"echo "O primeiro argumento é: $1"echo "O segundo argumento é: $2"echo "O terceiro argumento é: $3"echo "Meu PID é: $$"echo "Meus argumentos são: $@"echo "Meus argumentos são: $*"
When executed, it will produce the following output:
$ ./script um dois tresMeu nome é: ./scriptEu tenho 3 argumentos passados na minha execuçãoEste é o resultado da última execução: Eu tenho 3 argumentos passados na minha execuçãoO primeiro argumento é: umO segundo argumento é: doisO terceiro argumento é: tresMeu PID é: 6210Meus argumentos são: um dois tresMeus argumentos são: um dois tres
Variable $?
The variable $? represents the exit status of the previous command.
Example:
$ echo "Uirá"
Uirá
$ echo $?
0
In this example, the variable $? will show the return code of the previous command echo "Uirá".
It is conventionally established that when a command is executed without errors, it will return the return code 0 (zero). When there is an error, a value greater than 1 will be returned, and depending on the value, the command developer may provide a list of error codes and their meanings.
Variable $!
The variable $! will return the process ID of the last background command:
$ ls &[1] 27931
$ echo $!
27931
Do not confuse this with the variable $$
Variable $$
The variable $$ shows the ID of the current shell process. For shell scripts, this is the process ID in which they are being executed:
$ echo $$16487
$ ps -ef|grep 16487
16487 8098 0 junho/21 pts/18 00:00:01 bash
Variable $#
The variable $# returns the number of arguments the program received. It can be used in scripts when you want to know if the required number of arguments has been satisfied.
Using { }
Braces can be used to distinguish for the shell a variable that is used "within" text. See the example:
$ x="tri"
$ y="${x}ângulo"
echo $y
triângulo
Difference between $* and $@
- $* and $@ when used outside double quotes are identical and expand to the arguments.
- "$*" within quotes returns all arguments as if it were a single result separated by space. Arguments "One two" "three" will return "one two three", breaking the spaces between arguments, as if they were three arguments and not two.
- "$@" will return the arguments exactly as they were received. "One two" "three" will return as two arguments "One two" "three", without breaking the spaces.
Conclusion
If you are writing a shell script, it is very important that you know the special variables.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.