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: Greg Wooledge
Subject: Re: [Help-bash] Simple echo is printing the wrong value
Date: Wed, 4 Apr 2012 12:07:30 -0400
User-agent: Mutt/1.4.2.3i

On Wed, Apr 04, 2012 at 09:53:22AM -0600, Bill Gradwohl wrote:
> I purposely didn't use double quotes because I wanted it to create 2 passed
> parameters to the echo statement and 2 passed parameters to the function
> knowing that the white space character was there.

There is no white space character anywhere.  The unquoted parameter
expansion results in a list of words, and each word becomes one argument.

> If I put double quotes
> around it then it only passed 1 parameter, one with a space between the *
> and the X.

No.  There is no space in this:

file=*
func "$file"

The function is invoked with one argument, and that argument is a
string containing the single character '*'.

> Why do you say it is WRONG. I don't see it being wrong or right except
> depending on circumstances. If I want 2 passed parameters I can't use
> double quotes. Therefore quotes are optional depending on circumstances.

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.

> 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:

for file in *; do
  func "$file"
done

is if one of your filenames contains a space.

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

imadev:~$ args *.txt
29 args: <2009-06-03.txt> <bems.txt> <billcode.txt> ....

imadev:~$ mkdir junk && cd junk && touch '*' 'X' && args *
2 args: <X> <*>
imadev:~/junk$ file=*; args $file
2 args: <X> <*>
imadev:~/junk$ args "$file"
1 args: <*>



reply via email to

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