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.
Comments ( 0 )
No comments available