Shell function,

Source: Internet
Author: User

Shell function,
The following example defines a function and calls it:

#! /Bin/bashdemoFun () {echo "this is my first shell function! "} Echo" ----- function execution started ----- "demoFunecho" ----- function execution completed -----"
Output result:
----- Start to execute the function ----- This is my first shell function! ----- Function execution completed -----
The following defines a function with a return statement:
#! /Bin/bashfunWithReturn () {echo "This function adds two numbers to the input... "echo" Enter the first number: "read aNum echo" enter the second number: "read anotherNum echo" two numbers: $ aNum and $ anotherNum! "Return $ ($ aNum + $ anotherNum)} funWithReturnecho" returns the sum of the two numbers $? ! "
The output is similar to the following:
This function adds two numbers to the input... enter the first number: 1 enter the second number: 2 the two numbers are 1 and 2! The sum of the two numbers entered is 3!
The Return Value of the function is $? . Function parameters can be passed to functions in Shell. Inside the function body, get the parameter value in the form of $ n. For example, $1 indicates the first parameter, and $2 indicates the second parameter... a function example with a parameter:
#! /Bin/bashfunWithParam () {echo "the first parameter is $1! The second parameter of "echo" is $2! "Echo": the tenth parameter is $10! "Echo": the tenth parameter is ${10 }! "Echo" 11th parameters: ${11 }! The total number of "echo" parameters is $! "Echo" outputs all parameters as a string $ *! "} FunWithParam 1 2 3 4 5 6 7 8 9 34 73
Output result:
The first parameter is 1! The second parameter is 2! The tenth parameter is 10! The tenth parameter is 34! 11th parameters are 73! There are 11 parameters in total! Output all parameters as a string 1 2 3 4 5 6 7 8 9 34 73!
Note: $10 cannot obtain the tenth parameter. $ {10} is required to obtain the tenth parameter }. When n> = 10, you need to use $ {n} to obtain the parameter.

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.