help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Simple echo is printing the wrong value


From: Bill Gradwohl
Subject: Re: [Help-bash] Simple echo is printing the wrong value
Date: Wed, 4 Apr 2012 11:29:47 -0600

On Wed, Apr 4, 2012 at 10:07 AM, Greg Wooledge <address@hidden> wrote:
There is no white space character anywhere.  The unquoted parameter
expansion results in a list of words, and each word becomes one argument.

I realize I  explained it wrong.

Let me try again.

With 2 files in the directory, one named * and the other named nonAsterisk, the for loop:
for file in *; do
produces file='*' on the first pass.
The echo statement contains the unquoted $file which bash expands to 2 parameters namely * and nonAsterisk.

Same goes for the func line.

Therefore these 2 statements get 2 passed parameters which is what I wanted, and got.

Am I right so far? If so, then quoting is still optional isn't it?
 
echo $foo is always wrong because it never reproduces the content of
$foo correctly (except in degenerate cases).  The point of echo being
to present information to a human being, an echo that produces the
wrong information is horribly bad.

OK In the strictest sense you're right, but my example is contrived.
 
> That's why I eventually put double quotes around the second version to show
> only 1 parm is hitting echo and the function. That parm has a space in it.

You're confused.  The only way there can be a space in the argument to
the function in this code:

Yes - I made a mistake in my original explanation that I now corrected. I see you're correct.
 
If you are still confused about showing arguments, put markup around them
so you can be certain of what you are getting.  I use this script:

imadev:~$ cat bin/args
#! /bin/sh
printf "%d args:" $#
printf " <%s>" "$@"
echo

I habitually use echo when printf is so much more capable. Old habits die hard. I'll try switching to printf because it allows for the easy placement of, in your case, the less & greater symbols that automatically repeat if necessary.  Can't get that from echo.

Thanks.

--
Bill Gradwohl


reply via email to

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