The shell's functions are the same as Javacript and PHP's function declarations, except that when the shell calls the function, it can call the function simply by writing the function name, noting that parentheses are not appended to the function name.
Here is a simple function to use
#!/bin/bash# file name: Test.shfunction Test () { echo "aaaaaaa"} #直接使用函数名就可以调用函数testtest
Run:
If the function requires arguments, not the write arguments after the function name when the function is defined, but inside the function, like a command line line that uses $1-9 to get the arguments passed to the function. Then, when the function is called, arguments are given to the function, followed by the arguments directly after the function name, separated by a space between the arguments and function names.
#!/bin/bash# file name: Test.shfunction testone () { num=$1 tot=0 #求1到num的和 for ((i=1;i<= $num; i++)) { tot=$ (($tot + $i)) } echo $tot}function testtwo () { # echo ' expr $ + $ ' #等价于 echo $ (($1+$2))}testone 100TESTTWO 200 300
Run:
Shell Script--functions