Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the parameters of Shell function

Shulou Source: shulou.com Published: 2022-06-03 08:43:36 11月15日 Update

This article mainly introduces "how to understand Shell function parameters". In daily operation, I believe many people have doubts about how to understand Shell function parameters. 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 Shell function parameters". Next, please follow the editor to study!

In shell, you can pass parameters to a function when you call it. Inside the function body, the value of the parameter is obtained in the form of $n. For example, $1 represents the first parameter and $2 represents the second parameter.

Example of a function with parameters:

#! / bin/bashfunWithParam () {echo "The value of the first parameter is $1!" Echo "The value of the second parameter is $2!" Echo "The value of the tenth parameter is $10!" Echo "The value of the tenth parameter is ${10}!" Echo "The value of the eleventh parameter is ${11}!" Echo "The amount of the parameters is $#!" Echo "The string of the parameters is $*!"} funWithParam 1 2 34 5 6 7 8 9 34 73

Output:

The value of the first parameter is 1!

The value of the second parameter is 2!

The value of the tenth parameter is 10!

The value of the tenth parameter is 34!

The value of the eleventh parameter is 73!

The amount of the parameters is 12!

The string of the parameters is 1 2 34 5 6 7 8 9 34 73! "

Note that $10 cannot get the tenth parameter, and it takes ${10} to get the tenth parameter. When n > = 10:00, you need to use ${n} to get the parameters.

In addition, there are several special characters for handling parameters:

Parameter processing instructions $# the number of parameters passed to the script $* displays all parameters passed to the script in a single string $the current process ID number running by the script $! The last process running in the background has the same ID number $@ as $#, but uses it in quotation marks and returns each parameter in quotation marks. $- displays the current options used by Shell, which is the same 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. At this point, the study on "how to understand the parameters of the Shell function" 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!

Tags: Parameters functions learning scripts same commands characters quotes more processes errors handling help running practical special next number function background Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Apple Linux OPPO Reno Redmi