Command-line arguments
(Transferred from Http://c.biancheng.net/cpp/view/2739.html)
List of special variables
variables |
meaning |
$ |
File name of the current script |
$n |
Arguments passed to the script or function. N is a number that represents the first few parameters. For example, the first parameter is $ $, and the second argument is $ A. |
$# |
The number of arguments passed to the script or function. |
$* |
All parameters passed to the script or function. |
[Email protected] |
All parameters passed to the script or function. When enclosed by double quotation marks (""), it is slightly different from $*, as will be mentioned below. |
$? |
The exit state 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. |
The arguments passed to the script when the script is run are called command-line arguments. Command-line arguments are represented by a $n, for example, $ = for the first argument, and $ for the second argument, and so on
Take a look at the following script:
#!/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 : $#"
Operation Result:
$./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
the difference between $* and [email protected]
$* and [email protected] All represent all parameters passed to a function or script, and are 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 the parameters as a whole and outputs all parameters in the form of "$ $ ... $n"; "[email protected]" separates the various parameters to "$" "$" ... All parameters are output in the form "$n".
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 from "$*"
a b c d
print each param from "[email protected]"
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-command-line arguments