Linux shell functions

Source: Internet
Author: User
Tags function definition variable scope

The shell function is similar to a shell script, which holds a series of instructions, but the Shell function is in memory, not the hard disk file, so it is fast, and the shell can preprocess the function, so the function starts faster than the script.

1. Function definition

1234 function函数名() {    语句    [return]}

The keyword function representation defines a function, which can be omitted, followed by the function name, sometimes the function name can be followed by a parenthesis, the symbol "{" represents the entry of the function Execution command, the symbol can also be in the function name line, "}" means the end of the function body, between the two curly braces is the function body.

The statement part can be any shell command, or other functions can be called.

If you use the Exit command in a function, you can exit the entire script, which normally returns the part of the calling function to continue after the function ends.

You can use the break statement to interrupt the execution of a function.

Declare–f can display a list of defined functions

Declare–f can display only defined function names

Unset–f can remove functions from shell memory

Export–f output the function to the shell

In addition, the definition of a function can be placed in a. bash_profile file or in a script that uses a function, can also be placed directly on the command line, and the function can be deleted using the internal unset command. Once the user logs off, the shell will no longer hold these functions.

2. Invocation of function

Example of a function call:

12345678 #!/bin/bashfunctionshow() {    echo "hello , you are calling the function"}echo "first time call the function"showecho"second time call the function"show

3. Transfer of function parameters

Functions can pass parameters through positional variables. For example

Function Name parameter 1 parameter 2 parameter 3 parameter 4

When the function executes, it corresponds to parameter 1, and so on.

Instance:

12345678 #!/bin/bashfunctionshow() {    echo "hello , you are calling the function  $1"}echo "first time call the function"show firstecho"second time call the function"show second

4. return value of function

The keyword "return" in the function can be placed anywhere in the body of the function, usually to return certain values, the shell will stop execution after execution to return, return to the main program's call line, return value can only be an integer between 0~256, the return value will be saved to the variable "$?" In

Instance:

1234567891011121314151617181920 #!/bin/bashfunctionabc() {    RESULT=`expr$1 \% 2`   #表示取余数    if[ $RESULT –ne0 ] ; then        return0    else        return1    fi}echo "Please enter a number who can devide by 2"readNabc $Ncase$? in    0)        echo"yes,it is”        ;;    1)        echo“no ,it isn’t”        ;;esac

Here to pay attention to the parameters passed, the above read in the number, must be added to the $ symbol to pass to the function, I just started not know where is wrong, find a half-day to know is here error.

5. Loading of functions

If the function is in another file, how do we call it?

Here's a way. For example, the show function is written in function.sh, and we can use the source command

12 sourcefunction.shshow

This makes it possible to invoke the.

6. Deletion of functions

Usage: unset–f function name

7. Variable Scope of function

By default, a variable has a global scope, and if you want to set it as a local scope, you can add the local

For example:

1 locala="hello"

Using local variables allows the function to automatically release memory space occupied by variables after execution, thus reducing the consumption of system resources, and it is particularly important to define and use local variables when running large programs.

8. Nesting of functions

Functions can be nested, examples:

123456789101112131415 #!/bin/bashfunctionfirst() {    functionsecond() {        functionthird() {            echo"------this is third"        }        echo"this is the second"        third    }    echo"this is the first"    second}echo"start..."first

Linux shell functions

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.