Shell Introduction Function Application

Source: Internet
Author: User
Tags shebang

Recently in the study of shell programming, the text if there are errors in the place also hope that you criticize.

Let's start with a simple summation function.

#!/bin/bash#a test about function78f_sum(){   return $(($1+$23536);echo$total,$?;

Note Several questions:
1.shell is executed on a line-by-row basis, so it can be called after a function declaration, or there will be an error
2. We want to get the return value of the function can only be obtained by the $? variable can not get the return value.

Then look at the variables scope test:

#!/bin/bash#a test about functionecho $(uname)num=100uname(){   echo"my name is lenve"   ((num++))   return250}changeVar(){  local num=20   ((num++))   echo$num}uname;echo $?echo$num;changeVar;echo$num;

Output:

Points:
1. The first output of Linux is the result of the first line of code execution, which is called the system function
2. Call uname, the system will first call the function in this script, found the direct call, can not find the system function. Then output my name is Lenve
3.250 is the return value of the uname function
4.101 is due to Num's self-increment of 1 in the uname function.
5.21 means that if a variable with the same name as the outside of the function is defined in the function, the function internally uses the internally defined variable instead of the global variable
6.101 further substantiated the 5th

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

Shell Introduction Function Application

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.