Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the parameters of Shell function

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report