• 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 in Bash Script on Debian 12

  • 00:34 cat /etc/os-release
  • 00:44 vim function
  • 04:17 chmod +x function
  • 04:34 ls -la
  • 04:53 ./function arg1 argument2
  • 05:34 ls -la
  • 05:55 ./function arg1 2 3 4
  • 06:14 ls -la
{{postValue.id}}

To Define And Call Functions In Bash Script On Debian 12

Introduction:

Defining and calling a function in a Bash script on Debian 12 involves creating a function using the "function" keyword and then invoking it by its name.

Procedure:

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

root@linuxhelp:~/linuxhelp# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL=https://bugs.debian.org/

Step 2: Create a script using the function by using the below command.

root@linuxhelp:~/linuxhelp# vim function
#!/bin/bash

func1 () {
echo "The arguments are below 2, so creating file1"
touch file1
}

func2 () {
echo "The arguments are above 2, so creating file2"
touch file2
}

if [ $# -le 2 ]
then
	func1
else 
 	func2
fi

Step 3: Make the executable permission by using the below command.

root@linuxhelp:~/linuxhelp# chmod +x function

Step 4: Long list the files to check the executable permission and files by using the below command.

root@linuxhelp:~/linuxhelp# ls -la
total 12
drwxr-xr-x 2 root root 4096 Dec  7 04:52 .
drwx------ 8 root root 4096 Dec  7 04:52 ..
-rwxr-xr-x 1 root root  214 Dec  7 04:52 function

Step 5: Run the script file with less than or equal to 2 arguments by using the below command.

root@linuxhelp:~/linuxhelp# ./function arg1 argument2
The arguments are below 2, so creating file1

Step 6: Long list the files to check the created file by using the below command.

root@linuxhelp:~/linuxhelp# ls -la
total 12
drwxr-xr-x 2 root root 4096 Dec  7 04:54 .
drwx------ 8 root root 4096 Dec  7 04:52 ..
-rw-r--r-- 1 root root    0 Dec  7 04:54 file1
-rwxr-xr-x 1 root root  214 Dec  7 04:52 function

Step 7: Run the script file with less than 2 arguments by using the below command.

root@linuxhelp:~/linuxhelp# ./function arg1 2 3 4
The arguments are above 2, so creating file2

Step 8: Long list the files to check the created file by using the below command.

root@linuxhelp:~/linuxhelp# ls -la
total 12
drwxr-xr-x 2 root root 4096 Dec  7 04:55 .
drwx------ 8 root root 4096 Dec  7 04:52 ..
-rw-r--r-- 1 root root    0 Dec  7 04:54 file1
-rw-r--r-- 1 root root    0 Dec  7 04:55 file2
-rwxr-xr-x 1 root root  214 Dec  7 04:52 function

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 in the Bash Script on Debian 12. Your feedback is much welcome.

Tags:
michael
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What is a function in a Bash script?

A

A function in a Bash script is a reusable block of code that performs a specific task. It helps in organizing and modularizing your script.

Q

How do I define a function in a Bash script?

A

To define a function in a Bash script, follow these steps:
1. Start with the `function` keyword (optional).
2. Write the name of the function followed by parentheses.
3. Place the code block within curly braces `{ }`.
4. Optionally, you can provide input parameters within the parentheses.

Q

Can you provide an example of defining a function?

A

Certainly! Here's an example of how to define a function in a Bash script:


# Define a function that prints a greeting
function greet {
echo "Hello, world!"
}

Q

How do I call a function in a Bash script?

A

To call a function in a Bash script, simply write the name of the function followed by parentheses. You can place the function call anywhere within your script.

Q

Can you provide an example of calling a function?

A

Here's an example of how to call a function in a Bash script:


# Define a function
function greet {
echo "Hello, world!"
}

# Call the greet function
greet

Load more

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 Ryan ?
how to use visual traceroute tool

Am using traceroute command to check for the route. i got this tool while surfing. So pls help me out installation and usage of Visual traceroute tool.

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.