help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] The best way to get the last element of $@


From: Eric Blake
Subject: Re: [Help-bash] The best way to get the last element of $@
Date: Thu, 19 Mar 2015 11:02:29 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

On 03/19/2015 10:58 AM, Eric Blake wrote:
> On 03/19/2015 10:49 AM, 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.
> 
> Here's one way that requires no subshells (yes, it uses eval, but this
> is one case where we are safe from eval doing anything evil):
> 
> # set $last to the last member of $@
> eval printf -v last %s "address@hidden"

Although if address@hidden can be 0 (no arguments), then you'll need to add code
to special case that (unless you don't mind $last being set to $0, even
though $0 is not part of $@).

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]