BashSimilar to other scripting languages,BashIn this section, you can learn how to use functions to encapsulate logical block contentBash, and several examples of function-related functions.

  • What is a function
  • Functions in scripts
  • Parses the parameters of a function
  • When do you need to use functions

Introduction to the

Functions in Bash are snippets of code that can be reused, making it easier to reuse code. It differs from aliases in that aliases are suitable for encapsulating simple single commands, while functions can encapsulate complex multi-line commands. Functions are always executed in the current Shell, a major difference from scripts. Bash creates a new subshell to execute the script. If the function has the same name as the script, the function is executed first. However, functions take precedence over aliases, that is, if the function has the same name as the alias, the alias takes precedence.

grammar

Declaring a function in Bash is similar to declaring a function in JavaScript. The only difference is the way the function is called and passed arguments.

Function declaration

The declaration format of the function is as follows:

function FUNCTION() {
	[COMMAND]
}
Copy the code

Or omit the function keyword:

FUNCTION() {
	[COMMAND]
}
Copy the code

In the above code, FUNCTION is the custom FUNCTION name, and the FUNCTION code is enclosed in braces. So these two things are equivalent.

Note that the command statements in the function are on the same line. Separation, for example:

COMMAND1; COMMAND2

parameter

You can use parameter variables in the function body, but it’s important to note that variables declared in the function body can be declared in the entire script, that is, there will be a local variable declaration. By adding local to the variable declaration, you can limit the scope of variables to the function body.

Here are a few variables built into functions to get function arguments

  • ‘ ‘$n’ gets the NTH argument to the function
  • $0Getting the name of a function is the same as getting the name of a script in a script
  • $#Gets the total number of arguments to the function
  • $@Gets all arguments to the function, separated by Spaces
  • $*Gets all arguments to the function, using variables in betweenIFSBy default, Spaces are used. Custom symbols can also be used.

Add return value

We can also customize the return value within functions by using return to return the value from the function and then calling the script by using $? Gets the value returned by the function.

The sample

Example 1:

Simply use function output

#! /bin/bash

function hello() {
  echo "Hello world from function of bash"
}

hello
Copy the code

After executing the test file, we can see the output on the command line

Hello world from function of bash
Copy the code

As you can see from the above example, calling a function in Bash without adding parentheses works the same way as using a command in a script.

Example 2:

Use Bash’s functions and parse the arguments passed by the function

#! /bin/bash
user=mengj
function hello() {
  echo "Hello The ${1} from the function of bash"
}

hello ${user}

Copy the code

As we saw in the above example, passing parameters to a function in Bash simply adds parameters to the function name, which is the same as passing parameters to a command on the command line, isn’t it amazing?

The output of the script prints out the variable content of user as we expected

Hello mengj from the function of bash
Copy the code

When using a function’s parameters, we need to note that the function has its own scope. We get the parameters in the same way that we get the parameters in the script with ${n}. We get the parameters passed in, not the parameters passed in when the top-level scope executes the script.

Example 3:

Gets the value returned by the function

#! /bin/bash

function execute() {
	echo 'Run command in bash function'
	return 10
}

execute

echo "Get execute function returned value is: The ${? }"
Copy the code

After giving the script execution permission, we can see that the output of the script is as expected.

Run command in bash function
Get execute function returned value is: 10
Copy the code

More content

  1. See the Bash logic judgment article, Portal.
  2. See the Bash loop judgment article, Portal.