UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 45.13 Save Disk Space and Programming: Multiple Names for a ProgramChapter 45
Shell Programming for the Initiated
Next: 45.15 How to Unset all Command-Line Parameters
 

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


Previous: 45.13 Save Disk Space and Programming: Multiple Names for a ProgramUNIX Power ToolsNext: 45.15 How to Unset all Command-Line Parameters
45.13 Save Disk Space and Programming: Multiple Names for a ProgramBook Index45.15 How to Unset all Command-Line Parameters

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System