We can pass parameters to the script when we execute the Shell script. The format for getting parameters within a script is: $n. (n represents a number, 0 is the name of the shell script executed, 1 is the first argument to execute the script, 2 is the second argument to execute the script, and so on ...). )
Examples Show
We first write a script named test.sh, whose contents are as follows:
#!/bin/BashEcho "Shell Output script name and parameters";Echo "name of script executed: $";Echo "The first parameter is: $";Echo "The second parameter is: $-a";Echo "The third parameter is: $ $";
After we give the script permission to run, we run the output:
chmod +x test. SH $ . /test. SH 1 2 3 Shell Pass Argument instance! The file name to execute:. /test. SH The first parameter is: 1 The second parameter is: 2 The third parameter is: 3
Processing parameter Extensions
In addition to the above usage, we often use these:
parameter Handling |
Description |
$# |
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". |
$- |
Displays the current options used by the shell, the same as 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. |
Linux Learning notes-passing parameters to the Shell