Re: [Help-bash] The best way to get the last element of $@
From:
Greg Wooledge
Subject:
Re: [Help-bash] The best way to get the last element of $@
Date:
Thu, 19 Mar 2015 13:23:20 -0400
User-agent:
Mutt/1.4.2.3i
On Thu, Mar 19, 2015 at 11:49:48AM -0500, Peng Yu wrote:
> Hi,
>
> There are many ways of getting the last element of address@hidden Does anybody
> know what might be the best way to get the last element of address@hidden
> Thanks.
The BEST way is to reorder your arguments so the special one is first
instead of last, then shift it off. Like this:
foo() {
local x="$1"
shift
blah blah "$x" blah blah "$@"
}
If you can't do that (e.g. because you are writing a wrapper around cp),
then you can use various techniques. Here is one:
bar() {
local x="${@:(-1)}"
local argv=("${@:1:$#-1}")
blah blah "$x" blah blah "address@hidden"
}