Function in shell

Time:2021-1-18

Function allows us to divide a complex function into several modules, so that the program structure is clearer and the code reuse rate is higher. Like other programming languages, shell also supports functions. Shell functions must be defined before they are used.

1. Definition format of shell function

You can use function fun with the function keyword_ You can also give the function name fun directly_ Name () definition. Can not take any parameters, function name must be unique, so that can pass parameters, use$1,$2,...,$* ,[email protected]To get the parameters. It is recommended to use the function keyword to define the function, which is easy to read the code. The definition format is as follows:

function_name()
{
  list of commands
  [ return value ]
}

#Or
function function_name()
{
  list of commands
  [ return value ]
}

Function return value, you can explicitly increase the return statement; if not, the last command running result will be taken as the return value.

The return value of shell function can only be an integer, which is generally used to indicate the success of function execution. 0 indicates success, and other values indicate failure. If you return other data, such as a string, you will often get an error prompt: “numeric argument required”.

If you must let the function return a string, you can first define a variable to receive the calculation result of the function, and the script can access this variable to obtain the function return value when necessary.


#!/bin/bash

funcWorld()
{
    echo "world"
}
res=`funcWorld`
echo "hello $res"

Results of operation:

hello world

2. How to call shell function

To call a function in a script, you only need to give the function name. For example, to call the function funcworld () in the section “1. Definition format of shell function”, just give the function name funcworld, and the function name does not need to be followed by parentheses (). Shell functions are called in a way similar to shell commands.

3. Getting the return value of shell function

use$?obtain.$?You can not only get the exit status of the previous command, but also get the return value of the function. It can be seen that the call of shell function is very similar to the execution of shell command.

#!/bin/bash

add()
{
    varAddRes=`expr $1 + $2`
    return $varAddRes
}
Add 1 2 # call the function and pass parameters
echo $?

Results of operation:

3

4. Define local variables in shell function

The variable defined in shell function is global by default, and its scope starts from “the place where the variable definition is executed when the function is called” to the end of shell or the place where it is deleted. The variable defined by a function can be displayed as local by using the local keyword, and its scope is limited within the function. The local variable with the same name will shield the global variable. But notice that the arguments to the function are local.

#!/bin/bash

function test()
{
	Local localvar = dablelv # local variable
}
test
Echo $localvar # output is empty

5. Does Shell function need pre declaration

Shell function must be defined before use, there is no statement! A common approach is to put the function definition at the beginning of the script.

6. Deletion of shell function

Just like deleting variables, you can also use the unset command to delete functions, but add-fOptions, as follows:


unset -f function_name

7. Using functions on the command line

If you want to use a function on the command line, you need to execute the definition of the function on the command line. There is a disadvantage in defining shell functions directly on the command line. Once you exit the shell, the function definition will be invalid. You can define the function in. Bash in the home directory_ Profile file, or / etc / profile file that is valid for all users. In this way, every time after logging in, the profile file will be called, and you can call it immediately by entering the function name at the command prompt.

Note: to take effect immediately after modifying the file, it needs to be executedsource /etc/profileperhapssource ~/.bash_profileOtherwise, it will only take effect the next time you log in again.

The above is the details of the functions in shell. For more information about shell functions, please pay attention to other related articles of developer!