How to pass Multiple Arguments to Multiple Flags on Bash Script
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
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.
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
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 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
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.