Site icon DesignLinux

Bash Functions

Bash Functions

A Bash function is a predefined set of commands and can be use n number of times. It is useful to avoid repeatedly writing same code. In this tutorial, we will show you the basics of Bash functions.

Create Bash Functions

It is very easy and straightforward to create a bash function. You can declare bash functions in two different methods:

function_name () {
  commands
}

Single line version:

function_name () { commands; }
function function_name {
  commands
}

Single line version:

function function_name { commands; }

Following points need to keep in mind while creating the bash function:

Let’s see an example:

#!/bin/bash

hello_world () {
   echo 'hello, world'
}
hello_world

Here,

Variables Scope

As a developer or administrator you already aware about Variables scope. Let’s see variable scope in bash script.

The variables which can be accessed from anywhere in the script regardless of scope known as Global variables. In Bash, all variables by default are defined as global, even if declared inside the function.

Local variables can be declared within the function body with the local keyword and can be used only inside that function. You can have local variables with the same name in different functions.

For better understanding let’s see an example of variable scope in Bash:

#!/bin/bash

var1='A'
var2='B'

my_function () {
  local var1='C'
  var2='D'
  echo "Value of variable inside the function: var1: $var1, var2: $var2"
}

echo "Value before executing function: var1: $var1, var2: $var2"

my_function

echo "Value after executing function: var1: $var1, var2: $var2"

Here, we defined two global variable var1 and var2. After that there is an function which will set a local variable var1 and modifies the global variable var2.

It will show the following output once you run the script:

Value of variable inside the function: var1: A, var2: B
Value before executing function: var1: C, var2: D
Value after executing function: var1: A, var2: D

Return Values

In Bash functions you can’t return a value when it’s calling like other programming languages. But when the bash function completes, it will return the status of the last statement executed in function body. If success it will return 0 and non-zero decimal number in the 1 – 255 range for failure.

You can return the status using the return keyword and it will assign to the $? variable. The return statement ends the function. You can get function’s exit status.

#!/bin/bash

my_function () {
  echo "some output "
  return 12
}

my_function
echo $?
some output
12

It is easy method to assign the result of the function to a global variable.

#!/bin/bash

my_function () {
  func_result="some output"
}

my_function
echo $func_result
some output

There is an another best option for returning a value from a function, send the value to stdout using echo or printf:

#!/bin/bash

my_function () {
  local func_result="some output"
  echo "$func_result"
}

func_result="$(my_function)"
echo $func_result
some output

Here, it will print the message to stdout and assigning the function output to the func_result variable using the $() command substitution. You can use the variable later whenever needed.

Conclusion

A Bash function is a block of reusable code designed to perform a particular operation. Once defined, the function can be called multiple times within a script.

You can use the bash functions to create a shortcut for command for a lengthy commands.

Exit mobile version