Writing functions in the shell

Source: Internet
Author: User

  1. Execute the "nano function.sh" command to create a new shell script file with the name "function.sh".

    650) this.width=650; "class=" Exp-image-default "alt=" How to write Function "src=" in shell scripts http://f.hiphotos.baidu.com/exp/w=500/ Sign=7148a1e65adf8db1bc2e7c643923dddb/d000baa1cd11728b6a382c5cccfcc3cec3fd2cb2.jpg "style=" border:0px; "/>

  2. Edit the newly created shell script file "function.sh". The function must be defined before use.

    650) this.width=650; "class=" Exp-image-default "alt=" How to write Function "src=" in shell scripts http://d.hiphotos.baidu.com/exp/w=500/ Sign=5a0b357a5d82b2b7a79f39c401accb0a/95eef01f3a292df51bb1016bb8315c6034a8735a.jpg "style=" border:0px; "/>

  3. The contents of the shell script are as follows:

    #!/bin/bash

    Add () {

    sum_1=$1;

    sum_2=$2;

    sum= ' expr $sum _1 + $sum _2 ';

    echo "The sum is ${sum}";

    }


    Add $ A

    650) this.width=650; "class=" Exp-image-default "alt=" How to write Function "src=" in shell scripts http://c.hiphotos.baidu.com/exp/w=500/ Sign=7dcf946a46a7d933bfa8e4739d4ad194/dbb44aed2e738bd4c1601c4fa58b87d6277ff90f.jpg "style=" border:0px; "/>

  4. Assign executable permissions to the newly created function.sh command "chmod 755 function.sh".

    650) this.width=650; "class=" Exp-image-default "alt=" How to write Function "src=" in shell scripts http://d.hiphotos.baidu.com/exp/w=500/ Sign=fc00484931fae6cd0cb4ab613fb30f9e/3812b31bb051f819ed33aa34deb44aed2e73e7be.jpg "style=" border:0px; "/>

  5. Use "./function.sh 1 2" to invoke the script and pass two numbers to the script.

    650) this.width=650; "class=" Exp-image-default "alt=" How to write Function "src=" in shell scripts http://d.hiphotos.baidu.com/exp/w=500/ Sign=dc44388c791ed21b79c92ee59d6fddae/aec379310a55b319b0142b0b47a98226cefc17da.jpg "style=" border:0px; "/>

  6. The result of executing the command is "the sum is 3".

    650) this.width=650; "class=" Exp-image-default "alt=" How to write Function "src=" in shell scripts http://g.hiphotos.baidu.com/exp/w=500/ Sign=cc41cc41ddf9d72a1764101de42a282a/77094b36acaf2edd75c1e197891001e939019398.jpg "style=" border:0px; "/>



Writing functions in the shell

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.