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)"echo "${*: -1}"
whichshellcheck
won't complain about.echo "${@: -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...shellcheck
. I prefer your"${@:$#}"
as most readable. Cheers!shellcheck
is intended to mark non-POSIX syntax, it should complain.