Wednesday, May 28, 2008

Print last command line argument - bash

This is a small script to show how we can print the last command line argument passed to a script. Also printing the previous to last argument.

$ cat

eval last_arg=\$$#
echo Last Command Line Arg: $last_arg

((b4_pos=$#-1)); eval b4_last=\$$b4_pos
echo Previous to last arg: $b4_last


$ ./ A 2 B 3.4 D
Last Command Line Arg: D
Previous to last arg: 3.4

Similar Post:
How to access last argument of the previous command executed in bash prompt

1 comment:

daggoogle said...

Instead of using eval on $$#, ${!#} will give you the last command line argument directly.

© Jadu Saikia