How to define and Call Functions in Bash Script on Debian 12

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.

FAQ
Q
Are there any best practices for using functions in Bash scripts?
A
Here are a couple of best practices:
- Define your functions before calling them in your script for better readability.
- Give meaningful names to your functions that reflect their purpose.
- Use functions to encapsulate reusable blocks of code and improve script maintainability.
Q
Can I pass parameters to a Bash function?
A
Yes, you can pass parameters to a Bash function. Inside the function, you can access these parameters using the special variables `$1`, `$2`, `$3`, and so on.
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
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 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!"
}