Bash Shell Parameter Expansion does not work as expected for ${parameter:offset:length}

James Hanley jhanley at dgtlrift.com
Mon Jul 25 16:12:08 UTC 2016


Pulled from master, tested and confirmed to be fixed. Thanks.
-Jim

On Sun, Jul 24, 2016 at 9:58 PM, Denys Vlasenko <vda.linux at googlemail.com>
wrote:

> Thanks for the report. Fixed in git, please try it.
>
> On Fri, Jul 22, 2016 at 5:57 PM, James Hanley <jhanley at dgtlrift.com>
> wrote:
> > Since I'm unable to submit a bug via bugs.busybox.net due to some
> > certificate error, I'm posting it here:
> >
> >
> > When offset is a literal negative the variable produces and error "bad
> > substitution" when issuing the proper form with a space after for the
> first
> > : in ${parameter:offset:length} for a negative number. See
> >
> https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
> > subsection on ${parameter:offset:length}
> >
> > It should be noted that this does work when the offset parameter is a
> > variable instead of a literal with a negative number.  The script below
> > reproduces the issue and showcases the operation with a variable offset
> vs a
> > value offset.
> >
> > #!./busybox bash
> > set -x
> >
> > parameter=abcdef
> > offset=2
> > noffset=-2
> >
> > echo "parametert         '${parameter}'"
> > echo "var offset         '${parameter:${offset}}'"
> > echo "var noffset        '${parameter:${noffset}}'"
> > echo "val 2              '${parameter:2}'"
> > echo "val -2 (bad form)  '${parameter:-2}'"
> > echo "val -2 (good form) '${parameter: -2}'"
> > #EOF
> >
> > With this script busybox bash gives the following output:
> > ~/project/busybox $ ./test.sh
> > + parameter=abcdef
> > + offset=2
> > + noffset=-2
> > + echo parametert         'abcdef'
> > parametert         'abcdef'
> > + echo var offset         'cdef'
> > var offset         'cdef'
> > + echo var noffset        'ef'
> > var noffset        'ef'
> > + echo val 2              'cdef'
> > val 2              'cdef'
> > + echo val -2 (bad form)  'abcdef'
> > val -2 (bad form)  'abcdef'
> > ./test.sh: line 13: syntax error: bad substitution
> > ~/project/busybox $
> >
> > Changing the script to use the Linux Bash:
> > jim at jim-VirtualBox:~/project/busybox$ ./test.sh
> > + parameter=abcdef
> > + offset=2
> > + noffset=-2
> > + echo 'parametert         '\''abcdef'\'''
> > parametert         'abcdef'
> > + echo 'var offset         '\''cdef'\'''
> > var offset         'cdef'
> > + echo 'var noffset        '\''ef'\'''
> > var noffset        'ef'
> > + echo 'val 2              '\''cdef'\'''
> > val 2              'cdef'
> > + echo 'val -2 (bad form)  '\''abcdef'\'''
> > val -2 (bad form)  'abcdef'
> > + echo 'val -2 (good form) '\''ef'\'''
> > val -2 (good form) 'ef'
> > jim at jim-VirtualBox:~/project/busybox$
> >
> >
> >
> >
> > _______________________________________________
> > busybox mailing list
> > busybox at busybox.net
> > http://lists.busybox.net/mailman/listinfo/busybox
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20160725/2c65427f/attachment.html>


More information about the busybox mailing list