As mentioned above, variable names can only contain numbers, letters, and underscores, because some variables that contain other characters have special meanings. Such variables are called special variables.
For example, $ indicates the ID of the current shell process, that is, PID. See the following code:
Copy text-only new window
- $ Echo $
$echo $$
Running result
29949
Special Variable list
Variable |
Description |
$0 |
File Name of the current script |
$ N |
Parameters passed to the script or function. N is a number that represents the number of parameters. For example, the first parameter is $1, and the second parameter is $2. |
$ # |
The number of parameters passed to the script or function. |
$ * |
All parameters passed to the script or function. |
[Email protected] |
All parameters passed to the script or function. The double quotation marks ("") are slightly different from $. |
$? |
The exit status of the previous command or the return value of the function. |
$ |
The ID of the Current Shell Process. Shell scripts are the process IDs of these scripts. |
The parameter passed to the script when the command line parameter is run is called the command line parameter. The command line parameters are represented by $ n. For example, $1 indicates the first parameter, $2 indicates the second parameter, and so on.
See the following script:
Copy text-only new window
- #! /Bin/bash
-
- Echo "File Name: $0"
- Echo "first parameter: $1"
- Echo "first parameter: $2"
- Echo "quoted values: [email protected]"
- Echo "quoted values: $ *"
- Echo "Total number of parameters: $ #"
#!/bin/bashecho "File Name: $0"echo "First Parameter : $1"echo "First Parameter : $2"echo "Quoted Values: [email protected]"echo "Quoted Values: $*"echo "Total Number of Parameters : $#"
Running result:
$./test.sh Zara AliFile Name : ./test.shFirst Parameter : ZaraSecond Parameter : AliQuoted Values: Zara AliQuoted Values: Zara AliTotal Number of Parameters : 2
$ * And [email protected] $ * and [email protected] indicate all parameters passed to the function or script. If they are not included in double quotation marks, all use "$1" "$2 "... All parameters are output in the form of "$ N.
But when they are enclosed by double quotes (""), "$ *" takes all parameters as a whole, with "$1 $2... All parameters are output in the form of $ n. "[email protected]" separates parameters and uses "$1" "$2 "... All parameters are output in the form of "$ N.
The following example shows the difference between $ * and [email protected:
Copy text-only new window
- #! /Bin/bash
- Echo "\ $ * =" $ *
- Echo "\" \ $ * \ "=" "$ *"
-
- Echo "\ $ @ =" [email protected]
- Echo "\" \ $ @ \ "=" "[email protected]"
-
- Echo "print each Param from \ $ *"
- For VaR in $ *
- Do
- Echo "$ Var"
- Done
-
- Echo "print each Param from \ $ @"
- For VaR in [email protected]
- Do
- Echo "$ Var"
- Done
-
- Echo "print each Param from \" \ $ *\""
- For VaR in "$ *"
- Do
- Echo "$ Var"
- Done
-
- Echo "print each Param from \" \$ @\""
- For VaR in "[email protected]"
- Do
- Echo "$ Var"
- Done
#!/bin/bashecho "\$*=" $*echo "\"\$*\"=" "$*"echo "\[email protected]=" [email protected]echo "\"\[email protected]\"=" "[email protected]"echo "print each param from \$*"for var in $*do echo "$var"doneecho "print each param from \[email protected]"for var in [email protected]do echo "$var"doneecho "print each param from \"\$*\""for var in "$*"do echo "$var"doneecho "print each param from \"\[email protected]\""for var in "[email protected]"do echo "$var"done
Run./test. Sh "A" "B" "C" "D". The following result is displayed:
$*= a b c d"$*"= a b c d[email protected]= a b c d"[email protected]"= a b c dprint each param from $*abcdprint each param from [email protected]abcdprint each param from "$*"a b c dprint each param from "[email protected]"abcd
Exit Status $? You can get the exit status of the previous command. The exit status is the result returned after the previous command is executed.
The exit status is a number. Generally, if most commands are successfully executed, 0 is returned. If the command fails, 1 is returned.
However, some commands return other values, indicating different types of errors.
In the following example, the command is successfully executed:
$./test.sh Zara AliFile Name : ./test.shFirst Parameter : ZaraSecond Parameter : AliQuoted Values: Zara AliQuoted Values: Zara AliTotal Number of Parameters : 2$echo $?0$
$? It can also represent the return value of the function, which will be explained later.
Shell tutorial 3-shell special Variables