How to pass arguments in Bash Script on Debian 12

To Pass Arguments In Bash Script On Debian 12

Introduction :

Passing arguments in a Bash script allows you to provide input dynamically when running the script. Arguments are values passed to the script at the command line, enabling customization and flexibility.

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 and edit the file to make pass arguments script by using the below command.

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

echo $0 $1 $2 $3 $4

echo $@

echo $#

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

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

Step 4: Run the script file with the arguments by using the below command.

root@linuxhelp:~/linuxhelp# ./passargs arg1 arg2 arg3 arg4
./passargs arg1 arg2 arg3 arg4
arg1 arg2 arg3 arg4
4

Step 5: Edit the file to use while loop with pass arguments by using the below command.

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

#echo $0 $1 $2 $3 $4

#echo $@

#echo $#




while [ -f $1 ]
do
	echo "This script file $1 is exist"
done

Step 6: Run the script with arguments by using the below command.

root@linuxhelp:~/linuxhelp# ./passargs test
This script file test is exist
This script file test is exist
This script file test is exist
This script file test is exist
This script file test is exist
This script file test is exist

Step 7: Edit the file to use for loop with pass arguments by using the below command.

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

#echo $0 $1 $2 $3 $4

#echo $@

#echo $#




#while [ -f $1 ]
#do
#	echo "This script file $1 is exist"
#done



for args in $@
do
	echo "This is Arguments: $args"
done

Step 8: Run the script with arguments by using the below command.

root@linuxhelp:~/linuxhelp# ./passargs argument1 argument2 argument3 argument4
This is Arguments: argument1
This is Arguments: argument2
This is Arguments: argument3
This is Arguments: argument4

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to pass arguments in Bash Script on Debian 12. Your feedback is much welcome.

FAQ
Q
Can I pass flags or options to my script?
A
Yes, you can use flags like `-h` or `--help`. To process them, you can use tools like `getopts` or simply check the values of `$1`, `$2`, etc., within your script.
Q
How do I check if a specific argument is provided?
A
You can use conditional statements like `if [ -z "$1" ]` to check if the first argument is empty. Adjust the condition based on your specific requirements.
Q
Can I use descriptive names for my arguments instead of `$1`, `$2`, etc.?
A
While the special variables are commonly used, you can also use named parameters by assigning values to variables within your script. For example, `first_arg=$1` allows you to use `$first_arg` in your script.
Q
How many arguments can I pass to a Bash script?
A
You can pass as many arguments as needed. The variables `$1`, `$2`, etc., will represent the first, second, and so on, up to the total number of arguments.
Q
How do I pass arguments to a Bash script on Debian 12?
A
To pass arguments in a Bash script, you can use the special variables `$1`, `$2`, .., `$n`, where `$1` represents the first argument, `$2` the second, and so on. Here's a quick guide.