Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 5 Jun 2012 13:32:34 -0400
From:      Randy Pratt <bsd-unix@embarqmail.com>
To:        tundra@tundraware.com
Cc:        FreeBSD Mailing List <freebsd-questions@freebsd.org>
Subject:   Re: Possible /bin/sh Bug?
Message-ID:  <20120605133234.8b9e95e4.bsd-unix@embarqmail.com>
In-Reply-To: <4FCE287D.3090501@tundraware.com>
References:  <4FCE287D.3090501@tundraware.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 05 Jun 2012 10:40:45 -0500
Tim Daneliuk <tundra@tundraware.com> wrote:

> Given this script:
> #!/bin/sh
> 
> foo=""
> while read line
> do
>    foo="$foo -e"
> done
> echo $foo
> 
> Say I respond 3 times, I'd expect to see:
> 
> -e -e -e
> 
> Instead, I get:
> 
> -e -e

The last line "echo $foo" is what is getting confused.  At the end of
3 passes, $foo contains " -e -e -e" so when the last line is executed,
it looks like:

	echo -e -e -e

The first -e is probably being interperted by "echo" as a flag 
( echo -e ) and then only prints the last two -e.

Its easier to see if you execute the script with xtrace:

	sh -x /path/to/script

I'd recommend that you write the last line with quotes:

	echo "$foo"

and I think it'll produce the results you expect.

HTH,
Randy




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120605133234.8b9e95e4.bsd-unix>