Shell
Special variables: Shell $, $#, $*, [email protected], $?, $$ and command line arguments
As already mentioned, variable names can only contain numbers, letters, and underscores, because some variables that contain other characters have special meanings, and such variables are called special variables.
For example, $ represents the ID of the current shell process, the PID, as shown in the following code:
$echo $$
List of special variables
variable |
|
$0 |
current script filename |
$n |
$n arguments passed to a script or function $1 , the two parameter is $2 . |
$# |
arguments passed to script or function number |
$* |
passed to script or function all parameters . |
[email protected] Span style= "font-size:16px;font-family: ' Microsoft Jas Black ', Sans-serif" > |
passed to script or function all parameters . When enclosed by double quotation marks (""), it is slightly different from $*, as will be mentioned below. |
|
The exit status of the last command, or the return value of the function |
$$ |
The current Shell process ID. For Shell scripts, this is the process ID where the scripts are located. |
To illustrate:
1. #!/bin/bash
echo "File Name: $"
echo "first Parameter: $ $"
echo "first Parameter: $"
echo "Quoted Values: [Email protected]"
echo "Quoted Values: $*"
echo "Total number of Parameters: $#"
Operation Result:
$. /test.sh Zara Ali
File Name:./test.sh # $
First Parameter:zara #$1
Second Parameter:ali #$2
Quoted Values:zara Ali #[email protected]
Quoted Values:zara Ali #$*
Total number of Parameters:2 #$#
$* and [email protected] the difference
$* and [email protected] are all arguments passed to a function or script , not enclosed by double quotation marks (""), with "$" and "$" ... All parameters are output in the form "$n".
But when they are enclosed in double quotation marks ("") , "$*" takes all parameters as a whole , outputting all parameters in the form of "$ $ ... $n"; "[email protected]" Each parameter will be separated to "$" and "$" ... all parameters are output in the form "$n".
Note: The following example can clearly see the difference between $* and [email protected]:
#!/bin/bash
echo "\$*=" $*
echo "\" \$*\ "=" "$*"
echo "\[email protected]=" [email protected]
echo "\" \[email protected]\ "=" "[email protected]"
echo "Print each param from \$*"
For Var in $*
Do
echo "$var"
Done
echo "Print each param from \[email protected]"
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 \" \[email protected]\ ""
for Var in "[email protected]"
Do
echo "$var"
Done
Execute./test.sh "a" "B" "C" "D" and see the following result:
$*= a b c D
"$*" = a b c d
[Email protected]= a b c D
"[email protected]" = a b c d
Print each param from $*
A
B
C
D
Print each param from [email protected]
A
B
C
D
Print each param the from "$*" #"$* " will all parameters as a whole, output all parameters in "$ $ ... $n" form
A B c D
Print each param from "[email protected]" #"[email Protected]" will separate the various parameters to " $ "" $ "... all parameters are output in the form "$n" .
A
B
C
D
Exit status
$? you can get the exit status of the previous command. The so-called exit status is the return result after the last command was executed.
Exit status is a number, in general, most of the command execution succeeds returns 0, and the failure returns 1.
However, there are some commands that return other values that represent different types of errors.
In the following example, the command executes successfully:
$./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
$
Shell Special variables: Shell $, $#, $*, [email protected] , $, $ $ and command-line arguments