[Top][All Lists]
[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 13:58:12 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Wed, Apr 04, 2012 at 11:29:47AM -0600, Bill Gradwohl wrote:
> 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?
Yes, $file would expand to two words (and therefore two arguments in the
appropriate contexts) on the iteration where file=*.
But I do have a problem with "which is what I wanted", because it is
inconceivable that you could want this behavior. See below.
> If so, then quoting is still optional isn't it?
"for file in *" should be processing one file per iteration. Quoting
"$file" is mandatory. If you don't quote it, you could split a single
filename into multiple arguments, or expand glob characters in a filename
and produce a whole crapload of arguments.
for file in *; do
process $file # Wrong. You will end up passing the wrong args.
done
for file in *; do
process "$file" # Right.
done
Putting a filename into a string variable and then failing to quote that
variable when expanded is NEVER correct.
If your actual code is not "for file in * ..." but rather something like
"exts='*.png *.jpg *.gif'; for file in $exts ..." then that is a totally
different case, but you STILL have to quote "$file" even if you don't
quote $exts.
Side note: exts='*.png *.jpg *.gif' is a Bourne-shell-ism that should
not be used in Bash. Storing lists of things in a string variable with
spaces in between them is a hack to be used only in languages that do
not have arrays. Bash has arrays. But it's the only example I can come
up with that even resembles your remarks.
Side side note: when producing an example that demonstrates a problem,
don't mangle the original code so much that the example is no longer
relevant or descriptive.
- [Help-bash] Simple echo is printing the wrong value, Bill Gradwohl, 2012/04/03
- Re: [Help-bash] Simple echo is printing the wrong value, Eric Blake, 2012/04/03
- Re: [Help-bash] Simple echo is printing the wrong value, Bill Gradwohl, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Bill Gradwohl, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Greg Wooledge, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Bill Gradwohl, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Greg Wooledge, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Bill Gradwohl, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value,
Greg Wooledge <=
- Re: [Help-bash] Simple echo is printing the wrong value, Chet Ramey, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Bill Gradwohl, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Greg Wooledge, 2012/04/05
- Re: [Help-bash] Simple echo is printing the wrong value, Chet Ramey, 2012/04/05
- Re: [Help-bash] Simple echo is printing the wrong value, Bob Proulx, 2012/04/05
- Re: [Help-bash] Simple echo is printing the wrong value, DJ Mills, 2012/04/06
- Re: [Help-bash] Simple echo is printing the wrong value, Eric Blake, 2012/04/04