On 2006-12-27, Richard Neill <[EMAIL PROTECTED]> wrote:
> 1)substr support for a negative length argument.
> For example,
> stringZ=abcdef
> echo ${stringZ:2:-1} #prints cde
>
> i.e. ${string:x:y}
> returns the string, from start position x for y characters.
> but, if x is negative, start from the right hand side
> and if y is negative, print up to the end, -y.
>
> This would work the same way as PHP, and be extremely useful for, say,
> removing an extension from a filename.
If extension removal is all you need, you can already do it.
$ for f in *; do echo $f; done
Makefile.am
Makefile.in
ucl
$ for f in *; do echo ${f%.*}; done
Makefile
Makefile
ucl
See "Parameter Expansion" in the bash manual. Interestingly that same
section tells for '${parameter:offset}' expansion:
"If offset evaluates to a number less than zero, the value
is used as an offset from the end of the value of parameter."
However, if I use echo ${f:-3} I don't get the expected result. I wonder
if this is a bug of my bash version:
$ bash --version
GNU bash, version 3.00.16(1)-release (i486-pc-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.
$ for f in *; do echo ${f:-3}; done
Makefile.am
Makefile.in
ucl
It works with ${f:0-3}, which is odd, at least from my point of view.
_______________________________________________
Bug-bash mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-bash