This is the 24th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

This article on the shell function definition, use, return value for an example.

function

define

Bash shell function definitions need to be declared using function, in the following format:

function function_name

{

. Related code/command

}

This is written in a bash shell and may not be supported by other shells, such as dash, which does not recognize function.

In addition to that, there is another way to write:

function_name()

{

. Related code/command

}

Declare the function name with a pair of English parentheses () (this is common to bash and Dash, but not other scripts).

Function_name is the function name and must be unique. Functions need to be defined before they are used.

use

When used, just call the function name as if it were a command (no parentheses like in other languages like C). The following is an example:

#! /bin/bash

# Define the function hello
function hello
{
	echo "hello, world!"
}
Call the function hello
hello

Define function hello2
hello2()
{
	echo "hello, world2!"
}
Call function hello2
hello2
Copy the code

The result is as follows:

Function name duplication

The above function definition states that the function name must be unique. If not, to verify this, change the above example code to the same name: hello, as follows:

#! /bin/bash

# Define the function hello
function hello
{
	echo "hello, world!"
}
Call the function hello
hello

Define function hello2
function hello
{
	echo "hello, world2!"
}
Call function hello2
hello
Copy the code

Take a look at the execution result:

With the same name, the function defined later overrides the previous definition

Function argument usage

Even though a function is passing arguments, the function is defined in the format defined above. If you want to pass parameters when calling a function, you can use the same method as when executing a script, setting parameters separated by Spaces after calling the function as follows:

function_name arg1 arg2 arg3 …

If you want to get parameter values in the body of a function, use position variables.

The following is an example:

#! /bin/bash

# Define the function hello
hello()
{
	echo "Script name:$0"
	Print the values of the first three arguments
	echo "hello, The $1 $2 $3"
}
Call the function helloHello, Xiao Ming and Xiao HongCopy the code

Note that the value of $0 is the name of the script executed, not the function called. Get 3 arguments, but I only pass two, that is empty, the result is as follows:

Notice that although on the first line of the script I declare #! Use bash to explain, but when executing, I use bash to explain execution directly, which means #! The declaration does not take effect so that $0 can get the name of the current script being executed.

Function return value

There are three things to note about the return value of the function:

  1. The return value of the function can be $? Gets to represent the call status of the function: success/failure. By default, 0 indicates success and non-0 indicates failure, as in the command:
#! /bin/bash

# Define the function hello
hello()
{
	echo "Script name:$0"
	Print the values of the first three arguments
	echo "hello, The $1 $2 $3"
}
Call the function helloHello, Xiao Ming and Xiao Hongecho "Function result: $?"
Copy the code

Execution Result:

  1. Use return to return a specific status code, but the value must be between 0 and 255, as follows:
#! /bin/bash

less_than_0()
{
	return1}between_0_255()
{ 
	return100}eq_255()
{
	return255}greater_than_255()
{
	return 256
}
less_than_0
echo "less_than_0: $?"
between_0_255
echo "between_0_255: $?"
eq_255
echo "eq_255: $?"
greater_than_255
echo "greater_than_255: $?"
Copy the code

Execution Result:

Do not substitute 0-255 with other values.

  1. If the return value is not a number and we use $? Get the result as follows:
#! /bin/bash

fun_string_test()
{
	return "hello,world"
}
fun_string_test
echo "fun_string_test: $?"
Copy the code

Execution Result:

Call failed, return value must be numeric type