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.

VariableWhat 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.
$0Returns the name of the program executed.
$nreturns 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.

Leave a Reply 0

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?