Analysis of Shell custom functions and parameter calls

Source: Internet
Author: User

I. define functions

CopyCodeThe Code is as follows: function fname ()
{
Statements;
}

OrCopy codeThe Code is as follows: fname ()
{
Statements;
}

Ii. Call Functions

You can call a function by using the function name:
$ Fname; # execute a function

Parameters can be passed to the function and accessed by the script:
Fname arg1 arg2; # Passing Parameters

The following is the definition of the function fname. The function fname contains methods for accessing function parameters.

Copy codeThe Code is as follows: fname ()
{
Echo $1, $2; # access parameter 1 and parameter 2
Echo "$ @"; # print all parameters in a list at a time
Echo "$ *"; # similar to $ @, but the parameter is used as a single entity (string)
Return 0; # Return Value
}

Similarly, parameters can be passed to the script and accessed through Script: $0 (Script Name.

Annotations on function parameters in Bash:
$1 is the first parameter.
$2 is the second parameter.
$ N is the nth parameter.
"$ @" Is extended to "$1" "$2" "$3" and so on.
"$ *" Is extended to "$ 1C $ 2C $ 3C", where C is the first character of IFS.
"$ @" Is used most. Because "$ *" treats all parameters as a single string, it is rarely used.

Related Article

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.