Bash programming functions

Source: Internet
Author: User

Function: functions, libraries, and code reuse are easy to centrally manage. The purpose of code reuse is to complete structured programming. Function-defined statements are not executed, but are loaded into the memory. They can be called multiple times in the same shell process and can be passed when called, it will be executed during the call.

 

Two Methods for defining functions

  1. Function_name (){

... Nand

... CMD

}

  1. Function fuction_name {

... CMD

... CMD

}

* ** Functions can be nested and called by yourself!

 

* ** Function location parameters

Format of the called Function

Function_name arg1 arg2...

The function accepts the parameter and passes the parameter to the function. when calling the function, you can directly pass it to the function. In the function body, arg1 is $1, and arg2 is $2, and so on.

 

Exit status of the function: Generally the exit status of the last command in the function body;

 

Exit status of the custom function: return [N]; this command exits the function, does not execute subsequent commands, and exits with the specified status value [N.

 

Function execution result: You can use echo or printf statement to print the result (related variables )!

 

The range of the variable value specified in the function: the remaining script content! You can use the local command to clearly declare that the scope of the variable is the function body!

 

 


This article comes from the blog "Let me be more hungry", please be sure to keep this source http://ufirst.blog.51cto.com/7628670/1432614

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.