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
You should read it
May be interested
- Signal and Trap in Unix / Linuxsignals are software interrupt signals that are sent to a program that indicates a serious event has occurred. these events can be very diverse from user requests to illegal memory access. some signals, like signal interrupts, indicate that a user has asked the program to do something without being in control.
- Regular Expression in Unix / Linuxa regular expression is a string that can be used to describe different sequences (arrangement) of characters. regular expression is often used by various unix commands, including ed, sed, awk, grep and micro domains.
- Navigation IO in Unix / Linuxmost unix system commands receive input from your terminal and send the output back to your terminal. a command usually reads its input from a location called standard input, which happens to your terminal by default.
- Basic file system in Unix / Linuxa file system is a logical collection of files on a partition or a disk. a partition is an information store and can be combined into a hard disk if desired.
- Shell replacementshell does the magic when it encounters an expression that contains one or more special characters.
- Basic utilities: print, email in Unixby this chapter, you have a few basic insights about unix systems and some of its basic commands. this chapter will briefly discuss some of the basic but important utilities of unix utilities that you will use in your daily activities.
- User management in Unix / Linuxunix supports a concept of group account, which groups a number of accounts logically. each account will be part of any group account. the unix team plays an important role in executing process management and allowing access to files.
- Micro editor in Unix / Linuxthere are many ways to edit files in unix and for me, one of the best ways is to use the editor to edit the micro screen orientation. this editor lets you edit the lines of content with other lines in the file.
- C Shell operatorsthis tutorial lists all the operators available in c shell. here, most operators are similar to those we have in the c programming language.
- Control loop in Unix / Linuxthe previous chapter has been shown how to create loops and work with them to perform various tasks. sometimes you need to stop a loop or continue their repetitive process.