If I do:
set a b c
How do I access the last element of $@
, namely c
?
I've initially thought to subscript the $@
array:
"${@[$#-1]}"
But:
bash: ${@[$#-1]}: bad substitution
I eventually came up with:
eval echo "\$$#"
Is there a way to get the last element of $@
without eval
?
Is the only way to first copy it to another array and access ${copy[-1]}
?
echo "${@: -1}"
(Mind the space)" – muru Oct 29 '18 at 09:34echo "${*: -1}"
whichshellcheck
won't complain about. – Tom Hale Oct 29 '18 at 09:53echo "${@: -1}"
. What does it complain to you about? I'd go with"${@: -1}"
(or"${@:$#}"
) just because"$@"
is so much more common. Seeing"$*"
makes me stop and think what the code is doing... – ilkkachu Oct 29 '18 at 10:04shellcheck
. I prefer your"${@:$#}"
as most readable. Cheers! – Tom Hale Oct 29 '18 at 10:21shellcheck
is intended to mark non-POSIX syntax, it should complain. – schily Oct 29 '18 at 11:25