How to Pass Multiple Arguments with Multiple Flags in Bash Script on Debian 12

To Pass Multiple Arguments With Multiple Flags In Bash Script On Debian 12

Introduction:

Bash Script uses multiple arguments and flags to customize script behavior. Arguments are values passed to a script, while flags (options) modify the script's execution.

Procedure:

Step 1: Check the OS version by using following 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 file and make script for use multiple arguments with multiple flags by using following command.

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

while getopts "ab:c:d" opt; do
	case ${opt} in
	a)
		echo "Option a is print without any argument"
		;;
	b)
		echo "Option b is print only with argument"
		;;
	c)
		echo "Option c is print only with argument ${OPTARG}"
		;;
	d)
		echo "Option d is print only with argument ${OPTARG}"
		;;
	?)
		echo "Option is invalid"
		;;
	esac
done

Step 3: Make the Executable permission to the script file by using following command.

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

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

root@linuxhelp:~/linuxhelp# ls -la
total 36
drwxr-xr-x 2 root root 4096 Jan 23 04:38 .
drwx------ 8 root root 4096 Jan 23 04:38 ..
-rwxr-xr-x 1 root root  349 Jan 23 04:38 multiargsflags

Step 5: Run the script with a flag by using following command.

root@linuxhelp:~/linuxhelp# ./multiargsflags -a
Option a is print without any argument

Step 6: Run the script with b flag without any argument to check if b flag needs argument by using following command.

root@linuxhelp:~/linuxhelp# ./multiargsflags -b
./multiargsflags: option requires an argument -- b
Option is invalid

Step 7: Run the script with b flag and argument by using following command.

root@linuxhelp:~/linuxhelp# ./multiargsflags -b arg1
Option b is print only with argument

Step 8: Run the script with c flag(option) and argument by using following command.

root@linuxhelp:~/linuxhelp# ./multiargsflags -c arg1
Option c is print only with argument arg1

Step 9: Run the script with d flag and argument by using following command.

root@linuxhelp:~/linuxhelp# ./multiargsflags -d arg1
Option d is print only with argument 

Step 10 : Run the script with e flag to check e flag is valid or not by using following command.

root@linuxhelp:~/linuxhelp# ./multiargsflags -e
./multiargsflags: illegal option -- e
Option is invalid

Conclusion:

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

FAQ
Q
Are there any helpful tools or libraries for managing flags and arguments?
A
getopts built-in Bash function simplifies parsing options and arguments.

External libraries like argparse offer advanced argument parsing and validation features.
Q
Can I pass multiple arguments grouped under a single flag?
A
Yes, use quotes to group arguments for a flag (e.g., "-g "file1 file2" -v).

Ensure proper quoting to avoid interpreting spaces within arguments as separate flags.
Q
How to handle multiple flags for a single argument?
A
Use separate flags with dedicated logic (e.g., "-t type1" and "-t type2" for different processing).

Combine flags into one with a delimiter (e.g., "-c color:red,green,blue").
Q
What are common methods to pass multiple arguments with flags?
A
Positional arguments: Order matters. Flags and their arguments come sequentially (e.g., "-f file1 -v file2").


Optional arguments: Use getopts or custom logic to define flags with optional arguments (-f [filename]).


Arrays: Store arguments in an array and iterate through them based on flags (e.g., files=( "$@")).
Q
How to differentiate flags and arguments?
A
Flags: Start with "-". Modify script behavior but don't take additional values (e.g., "-v", "-h").

Arguments: Provide specific values to flags (-f "filename") or standalone inputs without flags (file1, file2).