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

Denys Vlasenko vda.linux at googlemail.com
Mon Jul 25 01:58:22 UTC 2016


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


More information about the busybox mailing list