• 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 on Bash Script

  • 00:45 cat /etc/os-release
  • 00:56 vim flagscript
  • 04:07 chmod +x flagscript
  • 04:19 ./flagscript -a welcome
  • 04:26 ./flagscript -a welcome -b boys
  • 04:38 ./flagscript -a welcome -b boys -c hello
  • 04:43 ./flagscript -a welcome -b boys -c hello -d hi
{{postValue.id}}

To Pass Multiple Arguments to Multiple Flags on Bash Script

Introduction

When developing a bash script, it is important to facilitate user interaction by allowing the inclusion of specific options or arguments. This can be accomplished in bash by utilizing flags or options. A flag is typically represented as a single character or a word that begins with a hyphen (-) and is employed to indicate a particular behavior or option.

Procedure

Step1: Check the OS-version

[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="9.4"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="9.4"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Oracle Linux Server 9.4"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:9:4:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 9"
ORACLE_BUGZILLA_PRODUCT_VERSION=9.4
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=9.4

Step2: Create the new file

[root@linuxhelp ~]# vim flagscript
#!/bin/bash
# Define a function to process the arguments
process_args() {
  while getopts ":a:b:c:" opt; do
    case $opt in
      a) echo "Argument a: $OPTARG" ;;
      b) echo "Argument b: $OPTARG" ;;
      c) echo "Argument c: $OPTARG" ;;
      \?) echo "Invalid option: -$OPTARG" ;;
    esac
  done
}
# Call the function and pass the arguments
process_args "$@"

Step 3: Change the permissions

[root@linuxhelp ~]# chmod +x flagscript

Step4: Run the script

[root@linuxhelp ~]# ./flagscript -a welcome
Argument a: welcome
[root@linuxhelp ~]# ./flagscript -a welcome -b boys
Argument a: welcome
Argument b: boys
[root@linuxhelp ~]# ./flagscript -a welcome -b boys -c hello
Argument a: welcome
Argument b: boys
Argument c: hello
[root@linuxhelp ~]# ./flagscript -a welcome -b boys -c hello -d hi
Argument a: welcome
Argument b: boys
Argument c: hello
Invalid option: -d

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 on Bash Script in Oracle Linux 9.4

Tags:
matthew
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

How to pass multiple arguments in a bash script?

A

You can pass more than one argument to your bash script. In general, here is the syntax of passing multiple arguments to any bash script: script.sh arg1 arg2 arg3 … The $2 variable will reference the second argument, the third argument is referenced by $3, .. etc.

Q

How do you pass all arguments to a function in bash?

A

To pass arguments to a Bash function, one can simply include them within parentheses when invoking the function. These arguments are then accessible within the function through the use of special variables known as positional parameters

Q

What variable would a bash script read to see how many arguments it was passed?

A

The script then uses the "echo" command to display the total number of arguments passed, and accessed through the "$#" variable. By executing this script with arguments, such as "./script.sh arg1 arg2", the output will be "The total number of arguments passed is: 2".

Q

How do you pass runtime arguments in a shell script?

A

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument, and so forth. The variable $0 references the current script.

Q

How to split a long bash command into multiple lines in a script?

A

You use \ for splitting. Some other recommendations make such a split more readable: add a two-space (or whatever your script already uses) indent at the beginning of each line. put | right after the indent.

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 Gibbson ?
How do i run both nginx and apache in same instance on centos

Hi...,

my server is based centos operating system and my webserver is already running on Apache.... i need to run both apache and nginx on same instance ... please help me to implement this concept...

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.