Bash Scripting for Beginners (Part 5 — Functions)

Ahmet Okan YILMAZ
3 min readApr 13, 2022

--

This is the last part of bash scripting. Let’s discuss functions.

Functions

A function can be defined as a set of commands which can be called several times within the script. The purpose of function is to help you make your scripts more readable and avoid writing the same code again and again.

Format:

function_name () { 
list of commands
}

or

function function_name () { 
list of commands
}

The name of your function is function_name, and that’s what you will use to call it from elsewhere in your scripts. The function name must be followed by parentheses, followed by a list of commands enclosed within braces.

Let’s look at a simple example:

#!/bin/bash# define your own function
Hello () {
echo "Hello World"
}
# call the function
Hello

Output:

$ ./test.sh
Hello World

Passing Arguments

Like any other programming languages, we can also pass the arguments. You can define a function that will accept parameters while calling the function.

In my first article about bash scripting, I talked about special variables and arguments. We will use those arguments in functions.

#!/bin/bash# define your own function
Hello () {
echo "Hello $1"
}
# call the function
Hello

Output:

$ ./test.sh "World"
Hello World

Return Values

Most other programming languages have the concept of a return value for functions. Bash functions don’t allow us to do this. However bash functions allow us to set a return status. The return status can be specified by using the return keyword, and it is assigned to the variable $?. The return statement terminates the function.

Example

#!/bin/bashmy_func () {
local func_result="some string"
echo "$func_result"
}

func_result="$(my_func)"
echo $func_result

Output:

$ ./test.sh
some string

Example:

#!/bin/bashprint_something () {
echo Hello $1
return 77
}
print_something Okan
echo return value $

Output:

$ ./test.sh
Hello Okan
return value 77

Nested Functions

One of the more interesting features of functions is that they can call themselves and also other functions. A function that calls itself is known as a recursive function.

#!/bin/bash

# Calling one function from another
first_func () {
echo "This is the first function."
second_func
}

second_func () {
echo "This is the second function."
}

# Calling first function.
first_func

Output:

$ ./test.sh
This is the first function.
This is the second function.

Variable Scope

When you create a variable inside the function or outside the function, it can be accessed globally. By default, variables are created in the global scope. This means that a variable created inside the function can be accessible globally. To make variables local to the function you can use the"local" keyword. The local keyword will restrict the variable scope from global to local.

#!/bin/bash

outside_function="This variable is from outside the function"

func1(){
local inside_function="This variable is from inside the func1"
}

func1
echo $outside_function
echo $inside_function

Output:

$ ./test.sh
This variable is from outside the function

Overriding Commands

To override the bash commands by creating a function with the same name as the command that we are going to override. For example, if we want to override the ls command, then we have to create a function with the name ls .

#!/bin/bash# override 'ls' command for 'ls -ltr'
ls () {
command ls -ltr
}
ls

Output:

$ ./test.sh
total 1
-rwxrwxrwx 1 kan kan 82 Apr 22 16:41 test

That’s the end of bash scripting for beginners. If you’d like an advance info, I reccomend Advanced Bash-Scripting Guide.

--

--

Ahmet Okan YILMAZ
Ahmet Okan YILMAZ

Written by Ahmet Okan YILMAZ

Industrial Engineer | Data Scientist | Factory Manager

Responses (1)