when executing a Shell script, parameters can be passed to the script, and the format of the parameters obtained in the script is: $n. n represents a number, 1 is the first argument to execute the script, and 2 is the second parameter that executes the script ...
The following example we pass three parameters to the script and output separately, where the file name is executed
# Cat Test.sh
#!/bin/bash
echo "Shell pass parameter Example";
echo "File name: $";
echo "First parameter: $";
echo "second parameter: $";
echo "The third parameter: $ A";
Output results
# sh test.sh 1 2 3
Shell Pass-Through parameters example
File name: test.sh
First parameter: 1
Second parameter: 2
Third parameter: 3
The following special characters are used to handle parameters:
Parameter handling instructions
$# The number of arguments passed to the script
$* Displays all parameters passed to the script in a single string.
such as "$*" in the Case of "" ", in the form of" $ $ ... $n "output all parameters.
$$ The current process ID number for the script to run
$! ID Number of the last process running in the background
[Email protected] Same as $*, but quoted when used, and returns each parameter in quotation marks.
such as "[email protected]" with "" "in the case, with" $ "" $ "... All parameters are output in the form "$n".
$- shows the current options that the shell uses, as is the SET command function.
$? Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error.
$* and [email protected] Difference:
The same point: all parameters are referenced.
Different points: only in double quotes. Assuming that three parameters 1, 2, 3 are written when the script is run, "*" is equivalent to "1 2 3" (one parameter is passed), and "@" is equivalent to "1" "2" "3" (three parameters are passed).
The following example:
# VI test.sh
#!/bin/bash
For i in "$*";d o
Echo $i
Done
echo "------------"
For i in "[email protected]";d o
Echo $i
Done
Output
# sh test.sh 1 2 3
1 2 3
------------
1
2
3
Shell scripts from getting started to complex three (passing parameters)