Shell custom Function and parameter invocation parsing _linux shell

Source: Internet
Author: User

One, the definition function

Copy Code code as follows:

function fname ()
{
statements;
}

Or
Copy Code code as follows:

FName ()
{
statements;
}

Second, call function

You can call a function by using only the function name:
$ fname; #执行函数

You can pass parameters to a function and be accessed by the script:
fname arg1 arg2; # Pass Parameters

The following is the definition of function fname. In function fname, a variety of methods of accessing function parameters are included.

Copy Code code as follows:

FName ()
{
echo $, $; # Access parameter 1 and parameter 2
echo "$@"; # Print all parameters at once in list mode
echo "$*"; # is similar to $@, but parameters are treated as a single entity (string)
return 0; # return value
}

Similarly, parameters can be passed to the script and accessed via script:$0 (script name).

Note about the function arguments in bash:
$ is the first parameter.
$ is the second parameter.
$n is the nth argument.
"$@" is extended to "$" "$" "$" and so on.
"$*" is extended to "$1c$2c$3c", where C is the first character of the 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.