• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to define and call functions on Bash Script on Ubuntu 22.04

  • 00:42 lsb_release -a
  • 00:58 vim function.sh
  • 02:26 chmod +x function.sh
  • 02:39 ./function.sh
  • 03:07 vim install_function.sh
  • 07:16 chmod +x install_function.sh
  • 07:38 ./install_function.sh apache2
  • 07:52 ./install_function.sh ssh
{{postValue.id}}

To Define And Call Functions On Bash Script On Ubuntu 22.04

Introduction

A shell function is similar to a function in C: It is a sequence of commands that do a single job. Typically, a function is used for an operation that you tend to do frequently in a shell script. Before you can call a function in a shell script, you must define it in the script.

Procedure Steps:

Step 1: Check the OS version by using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy

Step 2: Create a file by using vim editor

root@linuxhelp:~# vim function.sh
Add the following lines in that file
#!/bin/bash
simple_function()
{
        echo "This is the simple function"
}
simple_function

Step 3: Give execute permission to the file by using the below command

root@linuxhelp:~# chmod +x function.sh 

Step 4: Now run the script with prefix ./ by using the below command

root@linuxhelp:~# ./function.sh 
output :
This is the simple function

Step 5: Create an another file by using vi editor

root@linuxhelp:~# vim install_function.sh
Add the following lines in that file
#!/bin/bash
ret=`systemctl status $1 &> /root/output ; echo $?`
install_function()
{
        echo "Installing $1 service"
}
exit_function()
{
        echo "Service Already exist"
}
if [[ "$ret" = "0" ]]
then
        exit_function
else
        install_function
fi

Step 6: Give execute permission to the file by using the below command

root@linuxhelp:~# chmod +x install_function.sh 

Step 7: Now run the script with not installed service as argument by using the below command

root@linuxhelp:~# ./install_function.sh apache2
Output :
Installing  service

Step 8: Now again run the script with already installed service as argument by using the below command

root@linuxhelp:~# ./install_function.sh ssh
Output : 
Service Already exist

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to define and call functions on Bash Script on Ubuntu 22.04. Your feedback is much welcome.

Tags:
elijah
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What is the use of function in Shell Script?

A

A function is used for an operation that you tend to do frequently in a shell script.

Q

What is the syntax of the function?

A

Syntax :
functionName ()
{
Commands to be executed
}

Q

How to give execute permission to the file on Linux?

A

By using the following command you can give execute permission to the file
# chmod +x

Q

What is Shell Scripting Syntax?

A

A shell script is a text file that contains a sequence of commands for a UNIX-based operating system.

Q

How to pass a function in a Shell Script?

A

To invoke a function, simply use the function name as a command before that need to define a function.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Gibbson ?
How do i run both nginx and apache in same instance on centos

Hi...,

my server is based centos operating system and my webserver is already running on Apache.... i need to run both apache and nginx on same instance ... please help me to implement this concept...

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.