Bash function

Bash function

This chapter introduces the usage of Bash functions.

Introduction

A function is a code fragment that can be reused, which is conducive to code reuse. The difference between it and alias is that aliases are only suitable for encapsulating simple single commands, while functions can encapsulate complex multi-line commands.

Functions are always executed in the current Shell. This is a major difference from scripts. Bash will create a new sub-Shell execution script. If the function has the same name as the script, the function will be executed first. However, the priority of the function is not as good as the alias, that is, if the function and the alias have the same name, the alias is executed first.

There are two syntaxes for Bash function definitions.

# The first
fn() {
  # codes
}

# Second
function fn() {
  # codes
}

In the above code, fn is a custom function name, and the function code is written in braces. These two ways of writing are equivalent.

The following is an example of a simple function.

hello() {
  echo "Hello $1"
}

In the above code, $1 in the function body represents the first parameter of the function call.

When calling, write the function name directly, and the parameters follow the function name.

$ hello world
Hello world

The following is an example of a multi-line function that displays the current date and time.

today() {
  echo -n "Today's date is: "
  date +"%A, %B %-d, %Y"
}

To delete a function, you can use the unset command.

unset -f functionName

To view all functions defined in the current Shell, you can use the declare command.

$ declare -f

The above declare command will output not only the function name, but also all the definitions. The output order is in alphabetical order of function names. Since it will output a lot of content, it is best to use the pipe command with more or less.

The declare command also supports viewing the definition of a single function.

$ declare -f functionName

declare -F can output the names of all defined functions without the function body.

$ declare -F

Parametric variables

Parameter variables can be used in the function body to obtain function parameters. The parameter variables of the function are consistent with the script parameter variables.

-$1~$9: The first to ninth parameters of the function. -$0: The name of the script where the function is located. -$#: The total number of parameters of the function. -$@: All parameters of the function, separated by spaces. -$*: All the parameters of the function. The parameters are separated by the first character of the variable $IFS value. The default is a space, but it can be customized.

If the function has more than 9 parameters, the 10th parameter can be quoted in the form of ${10}, and so on.

Below is a sample script test.sh.

#!/bin/bash
# test.sh

function alice {
  echo "alice: $@"
  echo "$0: $1 $2 $3 $4"
  echo "$# arguments"

}

alice in wonderland

Run the script and the results are as follows.

$ bash test.sh
alice: in wonderland
test.sh: in wonderland
2 arguments

In the above example, since the function alice has only the first and second parameters, the third and fourth parameters are empty.

The following is an example of a log function.

function log_msg {
  echo "[`date'+ %F %T'` ]: $@"
}

The method of use is as follows.

$ log_msg "This is sample log message"
[2018-08-16 19:56:34 ]: This is sample log message

return command

The return command is used to return a value from a function. When the function executes this command, it will no longer be executed, and it will return directly.

function func_return_value {
  return 10
}

The function returns the return value to the caller. If the command line executes the function directly, the next command can use $? to get the return value.

$ func_return_value
$ echo "Value returned by function is: $?"
Value returned by function is: 10

return is not followed by parameters, it is also possible to only use it for return.

function name {
  commands
  return
}

Global variables and local variables, local command

The variables declared directly in the body of the Bash function are global variables and can be read by the entire script. This requires special care.

# Script test.sh
fn () {
  foo=1
  echo "fn: foo = $foo"
}

fn
echo "global: foo = $foo"

The running result of the above script is as follows.

$ bash test.sh
fn: foo = 1
global: foo = 1

In the above example, the variable $foo is declared inside the function fn, and it can also be read outside the function.

Not only can global variables be declared in the function body, but global variables can also be modified.

#! /bin/bash
foo=1

fn () {
  foo=2
}

fn

echo $foo

After the above code is executed, the value of the output variable $foo is 2.

You can use the local command to declare local variables in a function.

#! /bin/bash
# Script test.sh
fn () {
  local foo
  foo=1
  echo "fn: foo = $foo"
}

fn
echo "global: foo = $foo"

The running result of the above script is as follows.

$ bash test.sh
fn: foo = 1
global: foo =

In the above example, the $foo variable declared by the local command is only valid inside the function body, and is not defined outside the function body.