Explain the definition and call of shell function in detail

Time:2021-1-12

1、 Shell function definition format

Shell function definition format, each part is described as follows:

[function] and other parts enclosed in brackets indicate optional

your_ function_ The name part is the function name

your_ shell_ The commands part is function code; the shell generally takes the unit of behavior, but can use “\” to wrap the line, or use “;” as the separator in a single line

Return int part – the shell function can have a return value, but it can only return integers, not strings


[ function ] your_function_name()
{
  your_shell_commands;
  [return int;]
}

2、 Basic function definition and call

In this section, the program implements the function definition and call. Note that you should not use brackets to call the function (that is, testfun() is wrong). The code is as follows:


testFun{
  echo "helloworld!"
}

testFun

The operation is as follows:

3、 Pass parameter call

3.1 passing parameters without spaces

In this section, the program implements passing parameters to the called function, and $0 is the parameter passed by the shell running the script. Starting from $1, passing multiple parameters is separated by spaces. The code is as follows:


testFun(){
  echo $1
}

param="helloworld!"
testFun $param

The operation is as follows:

In addition to obtaining the nth parameter in the form of $n, you can also use the following writing methods:

form explain
$# The number of parameters passed to the script
$* Displays all parameters passed to the script in a single string
$$ The current process ID number of the script running
$! The ID number of the last process running in the background
[email protected] Same as $* but used in quotation marks and returns each parameter in quotation marks.
$- Displays the current options used by the shell, which has the same function as the set command.
$? Displays the exit status of the last command. 0 indicates no error, any other value indicates an error.

3.2 passing parameters with spaces

In this section, the program can pass parameters with spaces to the called function. We change “Hello world!” from “Hello world!” in the previous section to “Hello world!” as follows:


testFun(){
  echo $1
}

param="hello world!"
testFun $param

The operation is as follows:

You can see that only the “hello” before the space is printed, and the “world!” after the space is not printed (in fact, the “world!” after the space is recognized as $2)

It’s also easy to pass parameters with spaces. You can add double quotation marks when passing them (single quotation marks are not considered variables, so you can’t use single quotation marks). The code is as follows:


testFun(){
  echo $1
}

param="hello world!"
testFun "$param"

The operation is as follows:

4、 Get return value

4.1 return integer

testFun(){
  echo "helloworld!"
  return 99
}


#It should be noted that shell does not return the return value directly like other languages, and its return value is put in $? Which is why only integers can be returned
#So this method is wrong, and the value obtained is the echo printed content
# return_value=`testFun`
#The following is the correct way to get the return value returned by return
testFun
echo "the return value is: $?"

4.2 return string

We have always said that shell functions can only return integers, not strings. Let’s try to return strings by force. The code is as follows:


testFun(){
  echo "helloworld!"
  return "success"
}

testFun

Run as follows, you can see the error “bash: Return: Success: numeric argument required” at return

So it’s useless to return a string through return. We have two alternative methods. The first method is to get all the printings of the function through $(). The code is as follows:


testFun(){
  echo "helloworld!"
  echo "success"
}

return_value=$(testFun)
echo "$return_value"

The operation is as follows:

You can see that success is achieved. But at the same time, you can also see that all the output of the called function obtained by this method only needs to be parsed separately to return the value

Another way is to use a variable directly. The shell variables are global. The variables assigned in the called function can also be obtained at the calling function. The code is as follows:


testFun(){
  echo "helloworld!"
  return_value="success"
}

testFun
echo $return_value

The operation is as follows:

reference resources:

http://edu.jb51.net/linux/linux-shell-passing-arguments.html

The above is the detailed content of shell function definition and call. For more information about shell function definition and call, please pay attention to other related articles of developer!