How to understand the position variable parameters in Shell scripts
This article mainly introduces "how to understand the location variable parameters in the Shell script". In the daily operation, I believe that many people have doubts about how to understand the location variable parameters in the Shell script. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to understand the location variable parameters in the Shell script". Next, please follow the editor to study!
$#: number of parameters passed to the script
$*: displays all parameters passed to the script as a single string. Unlike location variables, this option can have more than 9 parameters
$: ID number of the current process in which the script is running
$!: the process ID number of the last process running in the background
$@: same as $#, but use it in quotation marks and return each parameter in quotation marks
$-: displays the current options used by shell, with the same function as the set command
$?: displays the exit status of the last command. 0 indicates that there is no error, and any other value indicates an error.
The code is as follows:
#! / bin/sh
# param.sh
# $0: full pathname of the file
Echo "path of script: $0"
# obtain the file name using the basename command file path
Echo "name of script: $(basename $0)"
# $1: parameter 1
Echo "parameter 1: $1"
# $2: parameter 2
Echo "parameter 2: $2"
# $3: parameter 3
Echo "parameter 3: $3"
# $4: parameter 4
Echo "parameter 4: $4"
# $5: parameter 5
Echo "parameter 5: $5"
# $#: the number of parameters passed to the script
Echo "The number of arguments passed: $#"
# $*: display the contents of all parameters I
Echo "Show all arguments: $*"
# $: the ID number under which the script is currently running
Echo "Process ID: $"
# $?: return code
Echo "errors: $?"
Enter. / param.sh hello world
The code is as follows:
[firefox@fire Shell] $. / param.sh hello world
Path of script:. / param.sh
Name of script: param.sh
Parameter 1: hello
Parameter 2: world
Parameter 3:
Parameter 4:
Parameter 5:
The number of arguments passed: 2
Show all arguments: hello world
Process ID: 5181
Errors: 0
At this point, the study on "how to understand the location variable parameters in the Shell script" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!