Categories: Shell Script

Special Variables in Linux $0 $? $$ $# [Basic Guide]

An important feature of shell scripts is the possibility of confirming execution with success of a given command.

Special return variables

It’s called a return variable.

The return variable is filled every time a command is executed. If the value returned is 0, the command was successfully executed.

If the value is different from 0, a problem was encountered.

If used in scripts, its value must be verified immediately after the execution of the desired command.

Variable What is for
$# Returns the number of arguments the program received.
$_ The underscore contains the absolute name of the shell file or script that is being executed as passed in the argument list.
$? Return variable when a program terminates 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 program executed.
$n returns the program’s n-number argument.
$* $@ Returns all arguments entered when running the program.

Take a look at the sample script where these variables are used:

#! /bin/bash 
echo “My name is: $0"
echo “I have $# arguments passed in my execution”
echo “This is the result of the last execution: $_”
echo “The first argument is: $1"
echo “The second argument is: $2"
echo “The third argument is: $3"
echo “My PID is: $”
echo “My arguments are: $@”
echo “My arguments are: $*”

When executed, it will produce the following result:

$./script one two three 
My name is:. /script 
I have 3 arguments passed in my execution 
This is the result of the last execution: I have 3 arguments passed in my execution 
The first argument is: one 
The second argument is: two 
The third argument is: three 
My PID is: 6210 
My arguments are: one two three 
My arguments are : one two three

Variable $?

The variable $? represents the exit status of the previous command.

Example:

$ echo “Uira”
Uira
$ echo $?
0

In this example the variable $? It will show the return code from the previous command echo “Will join”.

It has been agreed 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

Don’t confuse it with the variable $$

Variable $$

The $$ variable shows the current shell process ID. For shell scripts, this is the ID of the process in which they are running:

$ echo $ 16487
$ ps -ef|grep 16487
16487 8098 0 June/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 number of arguments required has been satisfied.

Using { }

Keys can be used to highlight to the shell a variable that is used “inside” a text. See the example:

$ x="Tri”
$ y="${x}angle”
$ echo $y
Triangle

Difference between $* and $@

  • $* and $@ when used outside of double quotes are identical and expand to the arguments.
  • “$*” in quotes returns all arguments as if they were a single result separated by a space. Arguments “One two” “three” will return “one two three”, breaking the spaces between the 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’re writing a shell script, it’s very important that you know the special variables.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.