This question already has an answer here:
I am trying to process command line arguments using
getopts in bash. One of the requirements is for the processing of an arbitrary number of option arguments (without the use of quotes).
1st example (only grabs the 1st argument)
madcap:~/projects$ ./getoptz.sh -s a b c -s was triggered Argument: a
2nd example (I want it to behave like this but without needing to quote the argument"
madcap:~/projects$ ./getoptz.sh -s "a b c" -s was triggered Argument: a b c
Is there a way to do this?
Here's the code I have now:
#!/bin/bash while getopts ":s:" opt; do case $opt in s) echo "-s was triggered" >&2 args="$OPTARG" echo "Argument: $args" ;; \?) echo "Invalid option: -$OPTARG" >&2 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done
Note that I want to support multiple flags this way, e.g.
madcap:~/projects$ ./getoptz.sh -s a b c -u 1 2 3 4 -s was triggered Argument: a b c -u was triggered Argument: 1 2 3 4