UNIX / Linux Special Variables - Unix/Linux

What are UNIX / Linux Special Variables?

In this section, we will argue in detail about special variable in UNIX. In one of our preceding chapter, we understand how to be careful when we use certain no alphanumeric font in variable names. This is because those fonts are used in the names of special UNIX variables. These variables are retained for specific functions.

For instance, the $ character represent the method ID number, or PID, of the current shell –

The above command writes the PID of the current shell –

The following table show a number of particular variables that you can use in your shell scripts −

S.No. Variable & Description
1
$0
The filename of the current script.
2
$n
These variables correspond to the arguments with which a script was invoked. Herenis a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).
3
$#
The number of arguments supplied to a script.
4
$*
All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
5
$@
All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
6
$?
The exit status of the last command executed.
7
$$
The process number of the current shell. For shell scripts, this is the process ID under which they are executing.
8
$!
The process number of the last background command.

Command-Line Arguments

The command-line arguments $1, $2, $3, ...$9 are positional parameters, with $0 pointing to the concrete command, program, shell script, or function and $1, $2, $3, ...$9 as the arguments to the command.

Following script uses different special variables connected to the command line –

Here is a sample run for the above script –

Special Parameters $* and $@

There are special parameters that allow access all the command-line influence at once. $* and $@ both will act the same unless they are covered in double quotes, "".

Both the parameters identify the command-line arguments. However, the "$*" particular parameter takes the entire list as one argument with spaces between and the "$@" special parameter takes the complete list and separate it into divide influence.

We can write the shell script as exposed below to procedure an indefinite number of command line influence with either the $* or $@ special parameters –

Here is a sample run for the above script –

Note − Here do...done is a kind of loop that will be covered in a subsequent tutorial.

Exit Status

The $? Variable represent the exit status of the preceding command.

Exit status is a mathematical value returned by every command upon its achievement. As a rule, most commands return an exit status of 0 if they were successful, and 1 if they were unsuccessful.

Some commands arrival additional exit statuses for exacting reasons. For instance, some commands make different between kinds of errors and will return different exit values depending on the particular type of breakdown.

Following is the instance of successful instruct –

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics