Pass parameters to Shell
- 原创
- 2017-11-05 09:14:52
- Memory, Renee
- 6951
Pass parameters to shell scripts when executing shell scripts. Use $n to get parameters in scripts. N stands for numbers, 0 is the executed shell script name , 1 is the first parameter executed in the script, 2 is the second paramenter execute, and so on.
Write a shell script and name it as test.sh which includes
#!/bin/bash output "Shell output script name and parameters"; output "Executed script name:$0"; output "First parameter:$1"; output "Second parameter:$2"; output "Third parameter:$3";
Run the script and output
$ chmod +x test.sh $ ./test.sh 1 2 3
Output
Executed script name:./test.sh First parameter:1 Second parameter:2 Third parameter:3
The followings are often used except what has been mentioned above.
Parameter | Note |
---|---|
$# | Numbers of parameters passed to shell |
$* | Use an unit string to display paramteres passed to all scripts. For example, "$*". Use「"」and output all parameters as "$1 $2 … $n". |
$$ | Current Process ID of the script. |
$! | Last process ID run at the backend. |
$@ | It is the same as $* but quotation marks are used to quote the paramenter returned. For example, "$@". Use「"」and output all parameters as "$1" "$2" … "$n". |
$- | Display current options Shell applies. Its feature is the same as set. |
$? | Display the status of last command Quit. 0 means no error, otherwise means there is an error. |
发表评论