[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Substring Expansion does not work as intended
From: |
Greg Wooledge |
Subject: |
Re: Substring Expansion does not work as intended |
Date: |
Wed, 20 Aug 2014 09:53:33 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Wed, Aug 20, 2014 at 03:05:48PM +0200, eckard.brauer@gmx.de wrote:
> "If offset evaluates to a number less than zero, the value is used as
> an offset from the end of the value of parameter."
>
> Repeat-By:
> x="abcdef"; echo "${x:-2}"
> Expected: ef
> Got: abcdef
This is a result of the collision of two different syntaxes. Bash is
using the ${parameter:-word} ("Use Default Values") syntax here.
There are two ways to work around this:
${x: -2}
${x:(-2)}
Either one of these avoids the :- syntax ambiguity. I prefer the
parentheses myself.