[lug] Losing what little Unix cred I once had

Daniel Webb lists at danielwebb.us
Fri Dec 15 00:54:39 MST 2006


On Thu, Dec 14, 2006 at 11:40:33PM -0600, Hugh Brown wrote:

> This is a curious one.  I'm assuming you are using bash from the $() 
> syntax.  echo is being provided by bash and is not the system /bin/echo

Oops, yes bash:

GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)

> As I was searching around for whether or not the bash builtin echo was 
> using the same octal <-> character table as you were expecting, it 
> occurred to me that you've told echo to enable the interpretation of 
> octal escapes.  So when you do a="$(echo -n -e '\0012')" you've told it 
> to take and run echo -n <return> and take the output and put it in a. 
> It looks to "a" as if you've just told it $(echo -n ).

Here's the heart of the matter:

$ echo -n "$(echo -n -e '\na')" |wc -c
2
$ echo -n "$(echo -n -e 'a\n')" |wc -c
1

>From the man page: 

"Bash  performs the expansion by executing command and replacing the command
substitution with the standard output of the command, with any trailing
newlines deleted."

-------------------------------------------------------

> I don't think I had any Unix cred, so I had none to lose :)

Mine was just pretend anyhow...  Try this out:

$ a="
"
$ echo "$a"

$ echo -n test | grep "$a" && echo match
test 
match

$ echo "$a" | wc -c
2

$ a=$'\n'
$ echo -n "$a" | wc -c
1

$ echo -n test | grep "$a" && echo match
test
match

ARG!

-------------------------------------------------------
I've got another weird problem:  if I put a local variable declaration before
a command substitution it doesn't redirect stderr any more:

a=$(some_command 2>&1)
<a has stdout and stderr from command as expected>

local a
a=$(some_command 2>&1)
<stdout goes to a but stderr prints to my console>

But as before, I'm hesitant to file a bug report because there is no doubt
some reason I'm missing that this is correct behavior!




More information about the LUG mailing list