• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Bash Functions

Written by Admin, Updated On November 8, 2020
bash, terminal
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:

  • Method 1: In this format function will starts with the name of function, followed by parentheses. This is the most used format.
function_name () {
  commands
}

Single line version:

function_name () { commands; }
  • Method 2: In this method, format will starts with function word, followed by the function name.
function function_name {
  commands
}

Single line version:

function function_name { commands; }

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

  • The commands between the curly braces ({}) are called the body of the function. The curly braces must be separate from the body by spaces or newlines.
  • To run the command you should use the function name. Function will execute the commands which are given in the function body whenever the function is called in the shell script.
  • Function must be defined before calling it.
  • When using single line functions, a semicolon ; must follow the last command in the function.
  • Keep your function name descriptive.

Let’s see an example:

#!/bin/bash

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

Here,

  • We have defined the function by giving it a name on line 3. The curly brace { indicate the starting of the function’s body.
  • It’s a body of function which starts from line number 4. Here you can declare multiple variable, commands, or statements.
  • Line 5, the closing curly bracket }, defines the end of the hello_world function.
  • You can execute the function as many times as you need. In line 7 we are executing the function.
  • When you will run the script, it will print hello, world.

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.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Create Bash Aliases
Next Article   Bash until Loop

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook