Bash Functions with Examples

Bash Functions with Examples
Bash Functions with Examples

Bash Functions with Examples

Basically bash function is a set of commands. Bash function mainly used for executing a single or group of commands again and again. Functions make it easier to read the code and execute meaningful group code statements. In this tutorial, we are going to learn Bash Functions with Examples.

Bash Function Syntax

You can declare a bash function in the following two formats:

Following is the first format for declaring bash function:

function_name() {
  commands
}

And below is the second format to declare bash function:

function function_name() {
  commands
}

Where,
function_name is the name of the function we are declaring and the commands are a list of commands that need to be executed when the function is called.

You should always write a list of commands inside { } curly braces and to execute these commands you should call the function.

IMPORTANT NOTE: You can write comments in bash by using # at the start of the line.

Following is a basic example for the bash function:

#!/bin/bash

# declaring welcome() function
welcome () {
   # list of commands
   echo 'welcome to linux4one'
}

# calling welcome function
welcome

The output should be:

welcome to linux4one

Defining Single Line Bash Functions

You define Single line Bash function in the following two formats:

function_name() { command;}

and

function function_name() { command;}

NOTE: when you are going to write the single line function you should always write semi-colon ; at the end of the command.

In the following example, the single line bash function is declared:

#!/bin/bash

# function declaration
welcome () { echo 'welcome to linux4one'; }
# calling function
welcome
Bash function with examples - single line
Bash function with examples – single line

The output should be:

welcome to linux4one

Variable Scope of Bash Functions

The are two types of variables in bash function Global Variables and Local Variables. A global variable can be defined anywhere in the bash script. You can also define a Global variable inside a function and can access anywhere in the script.

Where local variable can only be declared inside a function following keyword local. The Scope of local variable is limited to only inside the function. You can also define a local variable as the same name as the global variable and can access inside the function.

In the following example, global and local variables are declared:

#!/bin/bash

var_1='G1'
var_2='G2'

show_values () {
  local var_1='L1'
  var_2='G3'
  echo "Inside function: var_1: $var_1, var_2: $var_2"
}

echo "Values before executing function: var_1: $var_1, var_2: $var_2"

show_values

echo "Values after executing function: var_1: $var_1, var_2: $var_2"

The output should be:

Values before executing function: var_1: G1, var_2: G2
Inside function: var_1: L1, var_2: G3
Values after executing function: var_1: G1, var_2: G3
bash function with examples - variable scope output
bash function with examples – variable scope output

The above output shows that you can declare a local variable as the same name as a global variable. The scope of a local variable limited to function while you can access global variable out of function.

Passing Arguments in Bash Functions

You can pass arguments to the bash function easily by writing them with double quotes after function name separated by space. You can also put arguments without double quotes but in case of spaces used inside the argument, you should use double quotes.

Withing the bash function the arguments are accessible by using $1, $2, $3, …. $n depends on the position and number of parameters passed after the function name.

In the following example we have passed arguments to the function:

#!/bin/bash

function welcome() {
    echo "Welcome to $1"
}

welcome "Linux4one.com"

the output should be:

welcome to Linux4one.com
bash function with examples - passing arguments
bash function with examples – passing arguments

In bash functions $* or [email protected]  holds all the positional arguments passed to the function and $0 reserved for function name while $# reserved for the number of positional parameters passed to the function.

Bash Function Return Values

Like in other programming languages you can return the process value at the end of the function, You can not do such things in bash function. But you can return status of the function using return statement. This statement can return status for example 0 for success and values from 1 to 255 for failure. The return values can be accessed by using $? variable:

#!/bin/bash

welcome () {
  echo "Welcome to Linux4one"
  return 12
}

welcome
echo $?

The output should be:

Welcome tp Linux4one
12

In another way o return the value you can also declare and use a global variable at the end of function like given in below example:

#!/bin/bash

# declaring function
welcome () {
  echo "Welcome to Linux4one"
  # storing return values in result_value global variable
  result_value = "Website"
}

welcome
echo result_value

The output should be:

Welcome tp Linux4one
Website

Override Commands in Bash Function

You can declare a bash function as the same name as any normal commands in daily use. If you override such commands then to differentiate between commands and function name you should write command before the actual command like given below:

#!/bin/bash
    
# cat function declared
ping () {
  # as ping is actual command in below line you should put command statement before it.
  # executing ping command
  command ping www.google.com
}

# calling ping function
ping

Conclusion

You have successfully learned the Bash function with examples. If you have any of the queries regarding this then please don’t forget to comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here