Monday, May 6, 2013

[ Level 2 ] Get last argument in bash.

You could use the following methods to get the last argument that you pass to script.
#!/bin/bash
getLastArg1() {
  for last
  do
    true
  done
  echo $last
}

getLastArg2() {
  echo $@ | awk '{print($NF)}'
}

getLastArg3() {
  eval echo `echo \\$${#@}`
}

getLastArg1 $@
getLastArg2 $@
getLastArg3 $@
$ ./test.sh a b c
c
c
c
$

Wish this helps.

regards,
Stanley Huang