Possible /bin/sh Bug?

Randy Pratt bsd-unix at embarqmail.com
Tue Jun 5 17:32:43 UTC 2012

On Tue, 05 Jun 2012 10:40:45 -0500
Tim Daneliuk <tundra at 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.


More information about the freebsd-questions mailing list