Only for bashFrom oldest to newer solutions:
The most portable solution, even older sh
(sinceworks with spaces and glob characters) (no loop, faster):
eval printf "'%s\n'" "\"\${$#}\""
Since version 2.01) of bash
$ set -- The quick brown fox jumps over the lazy dog
$ printf '%s\n' "${!#} ${@:(-1)} ${@: -1} ${@:~0} ${!#}"
dog dog dog dog dog
For ksh, zsh and bash:
$ echoprintf '%s\n' "${@: -1} ${@:~0}" # the space beetwen `:`
# and `-1` is a must.
dog dog
And for "next to last":
$ echoprintf '%s\n' "${@:~1:1}"
lazy
ToUsing printf to workaround any issues with arguments that start with a dash (like -n
) use printf
:.
$ printf '%s\n' "${@:~0}"
dog
For all shells and for older sh
(works with spaces and glob characters) is:
$ set -- The quick brown fox jumps over the lazy dog "the * last argument"
$ eval echoprintf "'%s\n'" "\"\${$#}\""
The last * argument
Or, if you want to set a last
var:
$ eval last=\${$#}; echoprintf '%s\n' "$last"
The last * argument
And for "next to last":
$ eval echoprintf "'%s\n'" "\"\${$(($#-1))}\""
dog