Shell function: Shell function return value, delete function, call function at terminal

Source: Internet
Author: User



function allows us to divide a complex function into several modules, which makes the program structure clearer and the code reuse more efficient. Like other programming languages, the Shell supports functions as well. The Shell function must be defined before use.

The Shell functions are defined in the following format:


 
function_name () {
    list of commands
    [ return value ]
}


If you prefer, you can also add keyword functions to the function name:


 
function function_name () {
    list of commands
    [ return value ]
}


function return value, you can explicitly increment the return statement, if not added, the last command will run the result as the return value.

The Shell function return value can only be an integer, which is generally used to indicate success or failure of the function, 0 for success, and other values to fail. If you return other data, such as a string, you will often get an error message: "Numeric argument required".

If you have to make the function return a string, you can first define a variable to receive the result of the function, and the script accesses the variable when needed to get the function return value.

Let's look at an example:


#!/bin/bash

# Define your function here
Hello(){ echo "Url is http://www.so.com" }


#Invoke your function Hello


Operation Result:


URL is http://www.so.com


The calling function only needs to give the function name, and no parentheses are required.

Then look at a function with a return statement:


#!/bin/bash

funWithReturn(){
    echo "The function is to get the sum of two numbers..."
    echo -n "Input first number: "
    read aNum
    echo -n "Input another number: "
    read anotherNum
    echo "The two numbers are $aNum and $anotherNum"
    return $(($aNum+$anotherNum))
}
funWithReturn
#Capture value return by last command
ret=$?

echo "The sum of two numbers is $ret !"


Operation Result:


 
The function is to get the sum of two numbers...
Input first number: 3
Input another number: 4
The two numbers are 3 and 4
The sum of two numbers is 7 !


function return value after calling this function through $? To obtain.

Let's look at an example of a function nesting:


#!/bin/bash

# Calling one function from another
number_one(){
    echo "Url_1 is http://www.so.com"
    number_two
}

function number_two(){
    echo "Url_2 is http://mai"
}
number_one


As with the delete variable, the delete function can also use the unset command, but add the. f option as follows:


$unset. F function_name


If you want to invoke the function directly from the terminal, you can define the function in the. profile file in the home directory so that you can call it immediately after each login by entering the function name at the command prompt.






Shell function: Shell function return value, delete function, call function at Terminal


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.