From the Advanced Bash-Scripting Guide, to get the last command-line argument:

Or: lastarg=${!#} This is an indirect reference to the $# variable.
Note that lastarg=${!$#} doesn't work.


1 Answer 1


As it says it is an indirect reference. Take the following example:

$ var=test
$ test='Hello, world'
$ echo ${!var}
Hello, world

Now if I were to set the following positional parameters:

$ set -- one two three

And $# will represent the total number of positional parameters:

$ echo $#

This means when I call it like ${!#} I'm saying give me the value of the parameter named 3:

$ echo ${!#}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.