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: Bob Proulx
Subject: Re: [Help-bash] Simple echo is printing the wrong value
Date: Thu, 5 Apr 2012 16:49:22 -0600
User-agent: Mutt/1.5.21 (2010-09-15)

Bill Gradwohl wrote:
> file='*'
> echo $file          yields a long list of file names, but
> case $file in       doesn't.
> 
> It's the difference I was trying to highlight and figure out why * does
> different things under different circumstances.

Chet gave a good answer which I agree with.  But let me answer in a
different way and see if this different way of looking at it makes
sense to you.

'echo' used to be /bin/echo and an external command.  The fact that it
is a builtin today is just an implementation performance speedup.
Same thing for printf.  Moving a command from being an external
command to an internal builtin should not change the behavior in any
way.  Do you agree with that?  Otherwise if the behavior changed then
a shell that adds builtins to improve performance would cause scripts
to break.  That would be very bad.  So it should behave the same
whether it is an external command or an internal builtin.

If 'echo' is an external command then 'echo $file' must perform
argument parsing and everything on the option arguments and must
create an array of strings to pass as the argv array to the program in
the exec(2) call.  Those parameters must get there somehow.  The shell
will do word splitting and all of that on it the same as any external
command.  The * will go through file globbing and match a glob of
characters that matched files.  All expected, right?

Now 'case' has never been an external command.  The case construct was
always an internal builtin to the shell.  It was never an external
/usr/bin/case type of program.  The parameter isn't being processed
into an argument list for an external program.  Being internal it
doesn't word split nor do file globbing nor any of the other things
that are done for external programs.  Being internal the value is used
directly.  Being internal it doesn't need quoting.

Does that make sense why the syntax is different between those two
things?

Then go through every other syntax that you have been confused about
in the past and categories each of them into one or the other set.
Ask if they are an external or internal command.  Knowing that one
difference explains most of the behavior.

For example '[ ... ]' versus '[[ ... ]]'.  That will be your first
homework assignment.  Which of those are external and which of those
are external?  Hint: Use of 'type -a' is useful to help with this.
Does knowing this explain why one is doing word splitting, file
globbing, and other parameter actions that need quoting to prevent
while the other one doesn't?

Bob



reply via email to

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