Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get the last arg of command line?

397 views
Skip to first unread message

Ronald Bruck

unread,
Jun 26, 2008, 7:04:24 PM6/26/08
to
A newbie to bash asks a dumb question:

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

Dave B

unread,
Jun 27, 2008, 3:41:37 AM6/27/08
to
Ronald Bruck wrote:

${!#}

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.

0 new messages