English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
We can pass parameters to the script when executing a Shell script, and the format of obtaining parameters within the script is:$n.n Represents a number,1 The first parameter for executing the script,2 The second parameter for executing the script, and so on...
In the following examples, we pass three parameters to the script and output them separately, among which $0 The executed file name (including the file path):
#!/bin/bash # author:Basic Tutorial # url:www.oldtoolbag.com echo "Shell Parameter Passing Example!"; echo "The executed file name: $0"; echo "The first parameter is: $"1"; echo "The second parameter is: $"2"; echo "The third parameter is: $"3";
Set executable permissions for the script and execute the script, the output is as follows:
$ chmod +x test.sh $ ./test.sh 1 2 3 Shell Parameter Passing Example! The executed file name: ./test.sh The first parameter is:1 The second parameter is:2 The third parameter is:3
In addition, there are several special characters used to process parameters:
Parameter processing | Description |
---|---|
$# | The number of parameters passed to the script |
$* | Display all parameters passed to the script as a single string. As "$"*Cases enclosed in "" or starting with "$"1 $2 Output all parameters in the form of "… $n". |
$$ | The current process ID number of the script running |
$! | The ID number of the last process running in the background |
$@ | The same as $*The same as $, but it uses quotes when used, and each parameter is returned within the quotes. As in the case of "$@" enclosed in "", it is the same, but it is added with quotes when used, and each parameter is returned within the quotes.1" "2" … "$n" to output all parameters. |
$- | Displays the current options used by Shell, equivalent toset commandFunction is the same. |
$? | Display the exit status of the last command. 0 indicates no error, any other value indicates an error. |
#!/bin/bash # author:Basic Tutorial # url:www.oldtoolbag.com echo "Shell Parameter Passing Example!"; echo "The first parameter is: $"1"; echo "Number of parameters: $#"; echo "Passed parameters are displayed as a string: $"*";
Execute the script, the output is as follows:
$ chmod +x test.sh $ ./test.sh 1 2 3 Shell Parameter Passing Example! The first parameter is:1 Number of parameters:3 Passed parameters are displayed as a string:1 2 3
$* Difference from $@:
Similarities: Both refer to all parameters.
Differences: Only reflected in double quotes. Suppose three parameters were written when the script was running 1、2、3,,then " * " is equivalent to "1 2 3"(Passed one parameter), and "@" is equivalent to "1" "2" "3"(Passed three parameters).
#!/bin/bash # author:Basic Tutorial # url:www.oldtoolbag.com echo "-- \$* Demonstration ---" for i in "*"; do echo $i done echo "-- \$@ Demonstration ---" for i in "$@"; do echo $i done
Execute the script, the output is as follows:
$ chmod +x test.sh $ ./test.sh 1 2 3 -- $* Demonstration --- 1 2 3 -- $@ Demonstration --- 1 2 3