Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Bash Functions - DesignLinux

Nov 08 2020
designlinux 0 Comments
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.

Related

Tags: bash, terminal

Setting Up Hadoop Pre-requisites and Security Hardening – Part 2

Prev Post

Bash until Loop

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 8
  • 670
  • 1,055,442

DesignLinux.com © All rights reserved

Go to mobile version