I'd like to use getopts
inside a function that I have defined in my .bash_profile.
The idea is I'd like to pass in some flags to this function to alter its behavior.
Here's the code:
function t() {
echo $*
getopts "a:" OPTION
echo $OPTION
echo $OPTARG
}
When I invoke it like this:
t -a bc
I get this output:
-a bc
?
?
What's wrong? I'd like to get the value bc
without manually shifting and parsing. How do I use getopts
correctly inside a function?
EDIT: corrected my code snippet to try $OPTARG, to no avail
EDIT #2: OK turns out the code is fine, my shell was somehow messed up. Opening a new window solved it. The arg value was indeed in $OPTARG.
See Question&Answers more detail:os