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

Source: Internet
Author: User

The Shell also supports functions. 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:

    1. #!/bin/bash
    2. # Define your function here
    3. Hello () {
    4. Echo "Url is http://see.xidian.edu.cn/cpp/shell/"
    5. }
    6. # Invoke your function
    7. Hello

Operation Result:

$./test.shhello world$

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

Then look at a function with a return statement:

  1. #!/bin/bash
  2. Funwithreturn () {
  3. echo "The function is to get the sum of the numbers ..."
  4. echo- n "Input first number:"
  5. Read Anum
  6. echo- n "Input Another number:"
  7. Read Anothernum
  8. echo "The numbers is $aNum and $anotherNum!"
  9. return $ (($aNum + $anotherNum))
  10. }
  11. Funwithreturn
  12. # Capture Value Returnd by the last command
  13. RET=$?
  14. echo "The sum of numbers is $ret!"

Operation Result:

The function is to get the sum of the numbers ... Input first Number:25input Another number:50the, numbers are and 50! The sum of numbers is 75!

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

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

    1. #!/bin/bash
    2. # Calling one function from another
    3. Number_one () {
    4. Echo "Url_1 is http://see.xidian.edu.cn/cpp/shell/"
    5. Number_two
    6. }
    7. Number_two () {
    8. Echo "url_2 is http://see.xidian.edu.cn/cpp/u/xitong/"
    9. }
    10. Number_one

Operation Result:

Url_1 is http://see.xidian.edu.cn/cpp/shell/Url_2 is http://see.xidian.edu.cn/cpp/u/xitong/

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

    1. $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 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.