[Top][All Lists]

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

Re: weird echo behaviour...

From: Bob Proulx
Subject: Re: weird echo behaviour...
Date: Thu, 16 Sep 2004 09:40:40 -0600
User-agent: Mutt/1.3.28i

Alfred M. Szmidt wrote:
> Could someone explain the following behaviour for me?  Because I sure
> do not understand it.

You will be slapping your forehead as soon as you realize what is
happening.  This is normal behavior of the shell passing and
processing arguments.

> address@hidden:/tmp/foo$ touch 1 2 3 4 5
> address@hidden:/tmp/foo$ foo=`ls`

Since the output of ls is not a tty the -1 option is implied and ls
will list each file one per line.  Piped to 'wc -c' you would count 10
characters, five for the number and five for the newlines.  Piping to
'od -c' would show them character by character.

> address@hidden:/tmp/foo$ /bin/echo $foo
> 1 2 3 4 5

The shell's input field separator (IFS) is space, tab, and newline.
The shell's pass over the command line before argument splitting
expands the value of the variable.  The newlines separating the
filenames cause the the shell to split this into five arguments.

> address@hidden:/tmp/foo$ /bin/echo "$foo"
> 1
> 2
> 3
> 4
> 5

With double quotes the newline is now part of the echo argument.
There is only one argument to echo.  The newlines are now part of the

> address@hidden:/tmp/foo$ foo='1 2 3 4 5'
> address@hidden:/tmp/foo$ /bin/echo $foo
> 1 2 3 4 5
> address@hidden:/tmp/foo$ /bin/echo "$foo"
> 1 2 3 4 5
> address@hidden:/tmp/foo$ /bin/echo --version
> echo (GNU coreutils) 5.2.1


reply via email to

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