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