Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

linux
unix

What are special variables in UNIX/Linux?

Muhammad Ashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

widget

UNIX/Linux systems have special variables that are used internally by the shell and are available to all users.

Input to the bash script is provided in the following format:

./script.sh <input list>

Special Variable

Description

$0

The filename of the current script.

$n

"n" refers to a positive number that represents the nth argument passed to the script. For example, $1 represents the first argument.

$#

Represents the number of arguments passed to the script.

$*

Represents all the arguments passed to the script.

$?

Returns the exit status of the last command that was executed.

$!

Holds the process ID of the last background command.

$$

Represents the process ID of the current shell. For shell scripts, this is the process ID under which the scripts run.

$@

Represents all the arguments passed to the script.

The following bash script demonstrates these special variables and their usage. All scripts are run using the command ./script.sh This is the argument list.

#!/bin/bash
echo "Script name $0"
for ARGS in $@
do
let i=i+1
echo "Argument $i is $ARGS"
done
echo "Parameter list: (individually) $@"
echo "Parameter list: (as a single list) $*"
echo "Total number of parameters $#"
echo "Process ID $$"
widget

$@ and $*

These two variables hold the complete parameter list. The difference between them is that $@ holds each parameter individually, while $* holds all parameters as a single list of words separated by spaces.

#!/bin/bash
echo "Using \$*..."
for ARGS in "$*"
do
let i=i+1
echo "Argument $i is $ARGS"
done
echo -e "\nUsing \$@..."
for ARGS in "$@"
do
let i=i+1
echo "Argument $i is $ARGS"
done
widget

Exit status ($?)

The exit status of a command is a numerical value that is returned after the execution of the command. This numerical value indicates whether the command was executed successfully or not. Moreover, exit status can indicate different types of errors thrown in case the command was not executed successfully. As a rule of thumb, an exit status of 00 indicates success, and an exit status of 11 indicates failure.

echo "Executing echo command..."
if [ $? -eq '0' ]
then
echo "The last command executed successfully: Exit status $?"
else
echo "The last command was unsuccessful: Exit status $?"
fi

RELATED TAGS

linux
unix

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring