bug-bash
[Top][All Lists]
Advanced

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

Bash substrings: wish for support for negative length (read till n from


From: Richard Neill
Subject: Bash substrings: wish for support for negative length (read till n from end)
Date: Tue, 08 Jul 2008 04:31:36 +0100
User-agent: Thunderbird 2.0.0.12 (X11/20080306)

Dear All,

Substrings in bash contain 2 parameters, the start and the length.
Start may be negative, but if length is negative, it throws an error.
My request is that bash should understand negative length. This would be
useful in some occasions, and would be similar to the way PHP does it:
http://uk.php.net/manual/en/function.substr.php

For clarity, here are all the cases; the relevant ones are the last two:


$ stringZ=abcdef


$ echo ${stringZ:2}             #Positive start, no length
cdef                            #Reads from start.


$ echo ${stringZ: -2}           #Negative start, no length
ef                              #Reads 2 back from end.


$ echo ${stringZ:-2}            #No space before the -
abcdef                          #Is this what we expect?
                                #(or an unrelated bug?)


$ echo ${stringZ:2:1}           #Starts at 2, reads 1 char.
c


$ echo ${stringZ:2: -1}         #Wish: start at 2, read till
ERROR                           #1 before the end. i.e.
                                # cde

$ echo ${stringZ: -3: -1}       #Wish: start 3 back, read till
ERROR                           #1 before the end. i.e.
                                # de



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
   *  if y is negative, print up to (the end - y)



Thanks very much,

Richard




reply via email to

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