Special variables in Unix / Linux

The previous tutorial has warned about using special characters in your variable name. This is because these characters are used in the names of special variables in Unix. These variables are kept for special functions.

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

 $ echo $$ 

The above command will write the current shell's Process ID:

 29949 

Below is a table listing the special variables that you can use in Shell script:

File Description $ 0 File name of the current script. $ n These variables correspond to the parameters that a script is called. Here n is a positive integer decimal number corresponding to the position of a parameter (the first parameter is $ 1, the second parameter is $ 2 .). $ # Number of parameters provided for a script. $ * All parameters are double quoted. If a script takes two parameters, $ * is equivalent to $ 1 $ 2. $ @ All parameters are cited separately. If a script takes two parameters, $ @ is equivalent to $ 1 $ 2. $? The exit status of the previous command is run. $$ Number of current shell processes. For Shell script, this is the number of Process IDs they are running. $! Process number of the previous background command.

Command-line parameters in Unix / Linux

The command line variables $ 1, $ 2, $ 3, . $ 9 are positional parameters, with $ 0 pointing to the actual command, program, Shell script or function and $ 1, $ 2, $ 3, . $ 9 are parameter of that command.

The following script uses 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: $ # " 

Here is a sample run for the script above:

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

Special parameters $ * and $ @ in Unix / Linux

There are special parameters that allow access and all parameters of the command line at the same time. Both $ * and $ @ will work similarly unless they are surrounded by citations ("").

Both parameters define all the parameters of the command line, but $ * gets the entire list as a parameter with empty spaces in the middle and $ @ gets the entire list as a parameter and distinguishes them as parameters separately.

We can write Shell script as below to handle some unknown command line parameters with or special parameter $ * or $ @.

 #! / bin / sh for TOKEN in $ * do echo $ TOKEN done 

We run the template for the script above:

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

Note : Here because . done is a form of loop that we will discuss in the following tutorial.

Exit status (Exit) in Unix / Linux

$ Variable? represents the exit status of the previous command.

Exit status is a numeric value returned by each command when it is completed. As a rule, most commands return an exit status of 0 if they are successful and 1 if they fail.

Some commands return exit status with other additions for their own reasons. For example, some commands distinguish between types of errors and will return multiple exit values ​​depending on the type of error.

Here are examples of successful execution commands:

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

According to Tutorialspoint

Previous article: Use variables in Shell

Next lesson: Using array in Shell

ncG1vNJzZmismaXArq3KnmWcp51kwLGxwqKYpWWmlr%2BqrcGlnKxlmaN6trrIsWSloZ6qxQ%3D%3D