[Top][All Lists]

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

[Parameter Expansion] bug in ${variable% *}

From: Dashing
Subject: [Parameter Expansion] bug in ${variable% *}
Date: Mon, 11 Feb 2013 18:50:49 +0100

Bash version: 4.2.042 

I have a script that behaves erratically:
#! /bin/bash
last=${1##* }
rest=${1% *}
while [[ "${rest: -1}" == '\' ]]; do
        last="${rest##* } $last"
        rest=${rest% *}
        if [[ "$oldrest" == "$rest" ]]; then
                echo :--Mistake--:
#               sleep 0.01
#               rest=${rest% *}
#               if [[ "$oldrest" == "$rest" ]]; then
#                       echo 'unable to interpret'
#                       break
#               fi
echo REST:$rest:
echo LAST:$last:
$ ./pe 'mplayer foo1\ foo2\ foo3\ 4\ 5\ foo6\ 7'
LAST:foo1\ foo1\ foo1\ foo2\ foo3\ 4\ 5\ foo6\ 7:

What happens is that rest=${rest% *} doesn't always update $rest, 
even when there are spaces left in $rest.
Meanwhile last="${rest##* } $last" works every time.
In the above example it got stuck twice when $rest was mplayer 
foo1\. It tends to vary wildly.

I have commented out some code including sleep that sometimes helps 
make the parameter expansion work. This commented out code also 
avoids an infinite loop which is triggerable in a normal way with 
an argument of:
command\ filename


reply via email to

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