marcus-g
10/12/2017 - 10:17 PM

Find last bash script argument passed to it

from stackoverflow: http://bit.ly/2i9UWvB

This is a bit of a hack:

for last; do true; done echo $last This one is also pretty portable (again, should work with bash, ksh and sh) and it doesn't shift the arguments, which could be nice.

It uses the fact that for implicitly loops over the arguments if you don't tell it what to loop over, and the fact that for loop variables aren't scoped: they keep the last value they were set to.

some_func(){
  for last_arg; do true; done
  echo "last argument passed to some_func is: $last_arg"
}