12
2
Bash 4.2 added support for negative substring lengths:
http://tldp.org/LDP/abs/html/abs-guide.html#SUBSTREXTR01
Example 37-12. Negative parameter in string-extraction construct
When the "length" parameter is negative, it serves as an offset-from-end parameter.
For example, the following outputs "World" when tested on Ubuntu:
STR="Hello World!"
echo ${STR:6:-1}
But in OS X (GNU bash, version 4.3.42(1)-release (x86_64-apple-darwin14.5.0)), that usage produces the following error:
-1: substring expression < 0
Is this OS X specific, or was it disabled again in later bash updates? Is there a way to enable this functionality for OS X?
1http://tldp.org/LDP/abs/html/abs-guide.html#BASH2 - what was added when, starting with v2, v3 and v4 follows. – Hannu – 2016-01-30T03:42:11.060
@Hannu yes, that document says that the negative length semantics were added in version 4.2 – Brent Faust – 2016-01-31T03:21:41.267
I have the same issue : did you find a way to deal with that ? – Romain Jouin – 2017-01-03T13:50:45.270
@romainjouin Just by changing the algorithm. This syntax just doesn't work under OX X, even as of Bash version 4.3.46! Up-vote the question to try to get more eyes on this. Somebody knows the answer... – Brent Faust – 2017-02-12T02:44:04.003
1@Rubistro Just hit this :| One way around it is (in your example)
echo "Hello World" | rev | cut -c -5 | rev
– donovanmuller – 2017-04-14T23:51:01.367