• 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 to multiple flags using Bash Script on Rocky Linux 9.2

  • 00:32 Cat /etc/os-release
  • 00:47 vim script.sh
  • 01:55 ll
  • 02:06 chmod +x script.sh
  • 02:17 ll
  • 02:27 ./script.sh
  • 02:38 ./script.sh -x WELCOME -y TO -z LINUXHELP
{{postValue.id}}

To Pass Multiple Arguments To Multiple Flags Using Bash Script On Rocky Linux 9.2

Introduction:

In Bash scripting, flags are used to specify options for a script. Arguments are the values that are provided to those flags. When passing multiple arguments to a flag, you can either use a space-separated list of arguments or an array.

Procedure Steps:

Step 1: Check the OS version by using the below command.

[root@Linuxhelp ~]# cat /etc/os-release
NAME="Rocky Linux"
VERSION="9.2 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.2 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
SUPPORT_END="2032-05-31"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.2"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.2"

Step 2: Create a script file named script.sh by using vim text editor.

[root@Linuxhelp ~]# vim script.sh
Insert the following script to Pass Multiple Arguments to Multiple Flags using Bash Script
# Define the options (flags)
options="x:y:z:"
# Parse the command-line arguments
while getopts "$options" opt; do
    case $opt in
        x)
            x_args="$OPTARG"
            ;;
        y)
            y_args="$OPTARG"
            ;;
        z)
            z_args="$OPTARG"
            ;;
        ?)
            echo "Invalid option: -$OPTARG"
            exit 1
            ;;
    esac
done
# Print the extracted arguments
echo "x_args: $x_args"
echo "y_args: $y_args"
echo "z_args: $z_args"

Step 3: Long list the files by using the below command

[root@Linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1039 Aug 13 22:24 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Desktop
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Documents
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Downloads
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Music
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Pictures
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Public
-rw-r--r--. 1 root root  515 Nov 28 22:49 script.sh
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Templates
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Videos

Step 4: Now give the executable permission to the script file by using the below command

[root@Linuxhelp ~]# chmod +x script.sh 

Step 5: Again long list the files to check the permission of the script file which has given.

[root@Linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1039 Aug 13 22:24 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Desktop
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Documents
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Downloads
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Music
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Pictures
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Public
-rwxr-xr-x. 1 root root  515 Nov 28 22:49 script.sh
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Templates
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Videos

Step 6: Now run the script without flags by using the ./ command.

[root@Linuxhelp ~]# ./script.sh 
x_args: 
y_args: 
z_args: 

Our script shows the empty value

Step 7: Again run the script with the flags which is mentioned in the script file by using the below command

[root@Linuxhelp ~]# ./script.sh -x WELCOME -y TO -z LINUXHELP
x_args: WELCOME
y_args: TO
z_args: LINUXHELP
Now our script executes the correct value.

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to pass multiple arguments to multiple flags using Bash Script on Rocky Linux 9.2. Your feedback is much welcome.

Tags:
noah
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

How do I pass multiple arguments to a Bash Script using flags?

A

Use a while loop and case statement to manually parse the command-line arguments. Assign values based on flags and use shift to move through the argument list.

Q

Can I pass flags in any order?

A

Yes, when manually parsing arguments, you can pass flags in any order. However, with tools like getopts, the order of flags may matter.

Q

How do I handle unknown flags?

A

Include a default case in the case statement to handle unknown flags, displaying an error message and usage instructions.

Q

Should I provide a help option?

A

Yes, include a help option (-h or --help) that provides information on script usage and explains available flags to make the script user-friendly.

Q

Can I pass flags with or without values?

A

Yes, flags can be designed to accept values or be used as boolean switches without values. Adjust the script to handle both cases.

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 Lucas ?
Various options in Top command

Am using Top command only to view the load average, what are the various options in Top command..??

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.