• 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 Pass Multiple Arguments with Multiple Flags in Bash Script on Debian 12

  • 00:33 cat /etc/os-release
  • 00:46 vim multiargsflags
  • 04:12 chmod +x multiargsflags
  • 04:24 ls -la
  • 04:41 ./multiargsflags -a
  • 05:00 ./multiargsflags -b
  • 05:16 ./multiargsflags -b arg1
  • 05:50 ./multiargsflags -c arg1
  • 06:01 ./multiargsflags -d arg1
  • 06:40 ./multiargsflags -e
{{postValue.id}}

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.

Tags:
michael
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

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).

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 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

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

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.

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 legeek ?
Installation of the call center module

hello

I wish to install a call center in virtual with issabel, I downloaded the latest version of it , but I don' t arrive to install the call center module in issabel. please help me

thanks!

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.