[Chapter 45] 45.14 Finding the Last Command-Line Argument
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:54:37Z

#

| Chapter 45 Shell Programming for the Initiated |

## 45.14 Finding the Last Command-Line Argument

Do you need to pick up the last parameter `$1`, `$2` `...` from the parameter list [the "command line"-JP ]? It looks like `eval \$$#` would do it:

    eval
    $ set foo bar baz
    $ eval echo \$$#
    baz

except for a small problem with sh argument syntax:

>     $ set m n o p q r s t u v w x
>     $ echo $11
>     m1

`$11` means `${1}1`, and not `${11}`. Trying `${11}` directly gives `bad substitution`.

The only reliable way to get at the last parameter is to use something like

>     for i do last="$i"; done

[That for loop assigns each parameter to the shell variable named last; after the loop ends, `$last` will have the last parameter. Also, note that you won't need this trick on all sh-like shells. The Korn shell and bash understand `${11}`. -JP ]

- CT in comp.unix.questions on Usenet, 15 January 1990

45.13 Save Disk Space and Programming: Multiple Names for a Program |  | 45.15 How to Unset all Command-Line Parameters