Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX special variable


May 23, 2021 UNIX Getting started


Table of contents


Special variables

The previous tutorial used some non-character values as character variable names to warn you when naming variables. T his is because these characters are used as the names of special UNIX variables. These variables are reserved for specific functions.

For example, the $ character represents the ID code of the process, or the PID of the current shell:

    $echo $$

The above command outputs the PID of the current shell:

    29949

The following table lists some special variables that you can use in your shell script:

Variable Describe
$0 The file name of the current script.
$n These variables correspond to the parameters when a script is called. n is a heteth positive integer that corresponds to the position of a particular parameter $the first argument is $1, the second argument is $2, and so on).
$# The number of parameters provided to the script.
$* All parameters represent two references. If a script receives two parameters, that is, $?is equivalent to $1 $2.
$@ All parameters are referenced separately. If a script receives two parameters, that is, $1 is equivalent to $1 $2.
$? The exit state that executes the last command.
$$ The process number of the current shell. For shell scripts, the ID of the process they are executing.
$! The process number of the last background command.

Command-line arguments

Command line argument$ $1,$2,$3,......$9 are position parameters, and $0 points to actual commands, programs, shell scripts, or functions. $1,$2,$3,...... $9 as the command's argument.

The following script uses a variety of special variables related to the command line:

    #!/bin/sh

    echo "File Name: $0"
    echo "First Parameter : $1"
    echo "Second Parameter : $2"
    echo "Quoted Values: $@"
    echo "Quoted Values: $*"
    echo "Total Number of Parameters : $#"

This is an example of running the above script:

    $./test.sh Zara Ali
    File Name : ./test.sh
    First Parameter : Zara
    Second Parameter : Ali
    Quoted Values: Zara Ali
    Quoted Values: Zara Ali
    Total Number of Parameters : 2

Special parameters $* and $ @

There are special parameters that allow you to access all command line parameters. Unless they are included in double quotes, the $?and $-?run are the same.

Both parameters specify all command-line parameters, but the $?special parameter treats the entire list as a parameter, separated by spaces between values. The $-special parameter separates the entire list into separate parameters.

We can write a shell script that uses a special number of command-line parameters, such as $, or $?

    #!/bin/sh

    for TOKEN in $*
    do
       echo $TOKEN
    done

As an example, run the above script:

    $./test.sh Zara Ali 10 Years Old
    Zara
    Ali
    10
    Years
    Old

Note: Here do... Done is a loop, which we'll cover later in the tutorial.

Exit state

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

The exit state is the value returned by each command after it is completed. In general, most commands return 0 as an exit state if they succeed, and 1 as an exit state if they fail to execute.

Some commands return additional exit states for specific reasons. For example, some commands, in order to distinguish between different types of errors, return different exit values based on specific types of failure reasons.

Here's an example of a successful command:

    $./test.sh Zara Ali
    File Name : ./test.sh
    First Parameter : Zara
    Second Parameter : Ali
    Quoted Values: Zara Ali
    Quoted Values: Zara Ali
    Total Number of Parameters : 2
    $echo $?
    0
    $