bug-bash
[Top][All Lists]
Advanced

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

Re: bang-hash behavior change?


From: Greg Wooledge
Subject: Re: bang-hash behavior change?
Date: Thu, 26 Feb 2015 09:14:03 -0500
User-agent: Mutt/1.4.2.3i

On Wed, Feb 25, 2015 at 07:40:07PM -0500, Milo H. Fields wrote:
> echo " parens: ${!#}" 

> bash 4.1.17:
> 
> $ sh jnk arg1 arg2
> 
> plain: !#
> 
> parents: arg2

Can't say I've ever seen this syntax before.  It looks like you are
trying to get the last argument.  The syntax I've seen for getting the
last argument in bash is ${@:(-1)} (which has the advantage of being
generalizable to a larger set of uses -- see "Tangent" below).

imadev:~$ x() { echo "${@:(-1)}"; }; x foo 'last arg'
last arg

However, your syntax also does seem to work for me:

imadev:~$ y() { echo "${!#}"; }; y foo 'last arg'
last arg

imadev:~$ echo $BASH_VERSION
4.3.30(5)-release


Tangent: getting an arbitrary subset of arguments involves treating @
as an array and using the "range" notation, of which @:(-1) is just a
single case.

imadev:~$ z() { echo "First three args:" "${@:1:3}"; }; z a b c d e f g
First three args: a b c

imadev:~$ q() { echo "Last three args:" "${@:(-3)}"; }; q a b c d e f g
Last three args: e f g



reply via email to

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