If I use getopts to read command-line arguments, and I want to read
WHAT'S LEFT into a variable, how can I do this? For example: the line
is
doit -p hello goodbye
and I use getopts to read the "-p" and its parameter "hello", how can
I read "goodbye"? I don't know in advance how many arguments may be
on the command line. And at the end of execution OPTIND is defined but
OPTARG is not.
$# will give me the NUMBER of the last argument, but how to get the
argument itself? In C, I would just use argv[argc-1].
Logically, something like ${$#} should work, but it doesn't.
Thanks!
--Ron Bruck
${!#}
However, there may be cases where you have more than one argument left.
According to the manual, the right way to do what you want is
shift $(($OPTIND - 1))
printf "Remaining arguments are: %s\n" "$*"
that is, after the shift you can access the remaining arguments as $1,
$2...${!#}.
--
D.