Bash Functions

Bash Functions

The concept of the bash functions is similar to the functions, methods, procedures, or subroutines in the programming languages.

However, bash functions are limited in capabilities compared to those in the programming languages.

A function is a block of code that can be reused whenever needed.

In bash, we can use the functions to group the related and frequently used commands. The use of the bash function will ease the execution of repetitive commands.

This tutorial will cover different topics on bash functions like syntax, declaration, invocation, variable scopes, and arguments.

Creating Bash Functions

There are two ways to create bash functions. The first way is by using the function keyword, and the second way is without using the function keyword. Let’s explore the following syntaxes of bash function creation.

Creating Bash Functions Using the function Keyword

In order to create a bash function, you can use the function keyword before the name of your function. After the function name, write a parenthesis that takes arguments and a curly braces where the function body is included. The set of commands is written inside the curly braces. The function body should be separated from the curly braces by the new line for the following syntax.

function myFunc(){
    ......
    commands
    ......
}

Here, myFunc is the function’s name, and you are free to name it as you want it.

You can also use the one-liner format for the above function. The function body should be separated from the curly braces with a space in a one-liner format. It would be best if you did not miss the semicolon ; at the end of the last command in the body.

function myFunc(){ commands; }

Creating Bash Functions Without Using the function Keyword

You can also create the bash function without using the function keyword. You can write the name of your function followed by a parenthesis and a function body. The commands to be executed are written inside the function body.

myFunc(){
    ......
    commands
    ......
}

The one-liner format of the above function is shown below.

myFunc(){ commands; }

Defining and Calling Bash Functions

In the section above, you learned the syntax and the ways to create the bash function. But, only creating the function is not just enough. In order to execute the function, you should call the function. You can write the function name anywhere in the script to run the function. It should be noted that the function definition should always come before the function invocation. In the example below, we will demonstrate how to define and call a function with the following examples.

Code Example 1
function message1() {
    echo "welcome to the world of linux"
}

message1
Output 1
welcome to the world of linux
Code Example 2
message2() {
    echo "lets learn about bash scripting"
}

message2
Output 2
lets learn about bash scripting
Code Example 3
function message3(){ echo "bash functions"; }

message3
Output 3
bash functions
Code Example 4
message4(){ echo "explore more"; }

message4
Output 4
explore more

The bash function exits with an exit status of 0 when the commands inside it run successfully. The exit status denotes the exit status of the function’s last command.

Scope of Variables in Bash Functions

There are two scopes of the variables in bash scripting as in the programming languages. They are local and global scope. In bash, every variable declared has a global scope unless it is declared with the local keyword. Even if the variable is declared inside a function, it has a global scope.

The global variable can be accessed anywhere in the script, but the local variable is only available in the local scope. To understand it, let’s see the example below.

car="Toyota Hilux"
motorcycle="Husqvarna 250"
function display(){
    printf "Inside function call:\n";
    local car="Jeep Wrangler"
    motorcycle="CRF 300L"
    printf "car is $car\n"
    printf "motorcycle is $motorcycle\n"
}

printf "Before function call:\n";
printf "car is $car\n"
printf "motorcycle is $motorcycle\n"
display
printf "After function call:\n";
printf "car is $car\n"
printf "motorcycle is $motorcycle\n"

Here, the variables car and motorcycle hold the values Toyota Hilux and Husqvarna 250 at first. These variables have global scope. They can be accessed from anywhere in the script. First, we printed these two variables before calling the display function.

The display function contains a local variable car. And it has the value Jeep Wrangler. Similarly, the variable motorcycle is overridden with the value CRF 300L. These overridden variables are printed inside the function. The function is invoked. As a result, the new values are printed.

Next, the variables are printed outside the function after being executed. As the execution leaves the function, the value of the car variable resets to Toyota Hilux. It is because the car variable’s local scope only lasted within the function’s body. However, the value of the motorcycle variable remains CRF 300L as the motorcycle variable has a global scope inside the function. The value is overridden permanently.

The output of the program is shown below.

Before function call:
car is Toyota Hilux
motorcycle is Husqvarna 250
Inside function call:
car is Jeep Wrangler
motorcycle is CRF 300L
After function call:
car is Toyota Hilux
motorcycle is CRF 300L

Passing Arguments in Bash function

You can pass a number of arguments to the bash function like in the functions in programming languages. In programming languages like C, C++, or Python, you can pass the arguments in the parenthesis while invoking the function. But, in bash, you can write the arguments giving space after the function name. Spaces can separate the multiple arguments. Enclosing the arguments with quotes is a better practice.

There is no need to write the function parameters in the function definition. The parameters are automatically passed into the function, and they are in the form of $1, $2, $3….$n. These parameters correspond to the arguments passed in the function. For instance, the $1 parameter corresponds to the first argument. Similarly, the $# variable gives the number of the argument passed to the function. An example is shown below.

function info(){
    printf "my name is $1 and I am $2 years old\n"
    printf "the function has $# parameters\n"
}

info "Ram" "22"

Here, the variables $1 and $2 correspond to the arguments Ram and 22.

Output:

my name is Ram and I am 22 years old
the function has 2 parameters

Conclusion

In this tutorial, you explored the concept of functions in bash with the ways to define and call them. You also practically learned the concept of scopes and arguments in bash functions.

 

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)
0 Comments
Inline Feedbacks
View all comments
You May Also Like
How to Use the Kill Command in Linux
Read More

The Kill Command in Linux

Each Linux system runs several processes. Not every process behaves in the manner we want them to. Let’s…