zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: zsh-users@zsh.org
Subject: Re: why is eval needed?
Date: Sun, 20 Nov 2022 08:27:01 -0800	[thread overview]
Message-ID: <c717fdc0-7661-3387-d0c8-3aff56d7f6c7@eastlink.ca> (raw)
In-Reply-To: <20221120150848.74di3mpjwnfcul34@chazelas.org>


On 2022-11-20 07:08, Stephane Chazelas wrote:
> zsh doesn't impose anything. "-L 2" is a string made of 4
> characters in any shell or programming language. There's no
> programming language where "-L 2" means 2 strings -L and 2.

But that's the thing, I'd naively thought the variable could be inserted 
in the command string and be 'just' a string of characters, but zsh 
imposes that 'tree' must see '-L 2' as a single entity, yes?  As shown, 
we need word splitting to solve the problem and show tree what it wants 
to see. Or, as I was speculating, some way of just flattening the string 
back to nothing more than a string of characters -- but then again, 
probably tree wouldn't like that either, perhaps the string is always 
'packaged' into words?  If so, then there's no avoiding that we must 
word-split '-L 2' into two words and there's nothing to wish for.


> That is a very weird feature from a language design point of
> view. Anybody who doesn't know what the shells looked like
> before the Bourne shell would think Stephen Bourne was out of
> his mind.
>
It's what happens when functionality expands by accretion -- simple 
structures no longer suffice but they are tweaked rather than 
rethought.  It's like the difference between the layout of Paris vs. 
London.
> My understanding is that the Bourne shell's bizarre IFS handling
> and the fact that globbing was performed upon parameter
> expansion was an attempt to keep some level of backward
> compatibility with that Thompson shell. You see similar things
> hapenning in csh from the same era.
The dilemma of compatibility!  No easy answers.  I myself tend towards a 
'garage to the dump' mentality but it's not that simple.
> The Korn shell (from the early 80s) kept most of that with the
> exception that it only did IFS-splitting upon expansions, not on
> literal text.
Holy cow!  IFS splitting in text??
> The fact that in sh/ksh/bash:
>
> arg='-L 1'
> tree $arg
>
> Calls tree with "-L" and "1" as arguments is not that bash
> doesn't do some sort of magic "grouping" that zsh would be
> doing, but that ksh/bash contrary to zsh does that extra layer
> of $IFS-splitting from the Bourne shell on top of the syntax
> parsing as the default value of $IFS happens to contain the
> space character.
Ah!  So my little issue is a zsh exclusive?  Not complaining tho, I 
don't like zsh doing things I didn't ask it to do so if in this case I 
need to request a word split that's just fine.  After all it's tree 
that's being difficult so the problem is rare and easily solved.
> That's why in sh/bash/ksh you almost always need to quote
> parameter expansions if you intend to pass the contents of a
> variable as an argument to a command.
>
> See
> https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells
> for the kind of thing that  can happen if you forget.
Heavy.  That's the closest I've come to understanding those vulnerabilities.
>
> If you want to assemble a string, and that string to be
> evaluated as shell code, that's what eval is for. eval evaluate
> code written in the shell language. That's a way  to dynamically
> invoke the language interpreter.
Ok, so I understood that correctly.  At least partially.
> What are you doing there?  I have no 'nm' command here.  No such command in
> Debian repository.
> nm is a standard development command (though not -D which AFAIK
> is a GNU extension). Part of GNU binutils on GNU systems.
Ah, that's why I can't find it.  It's hard to find commands so packaged.
> Note that tree is not part of the GNU project, it's just a
> utility written by some guy and shared to the world. There is
> *some* level of consistency among utilities in the GNU
> toolchest. There is even such a thing as published GNU coding standards.
> See for instance
> https://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html#Command_002dLine-Interfaces
Yeah, tides of organization wash here and there.  No overall commander 
tho, so a bit of chaos is unavoidable.
> I'll agree with you that the lack of consistency in API between the
> different tools that are available out there can be annoying
> (nothing to do with GNU or Linux)
You know, half of it is just cultural adaptation.  When I first started 
with Linux the first thing I did was decide between zsh and bash and I'm 
basically assuming that Linux will be like DOS but slightly more 
powerful and zsh will be like C and that all Linux OS apps will be as 
flawless as WordPerfect 5.1.  CRASH!  Now that I'm used to being in a 
bazaar not a monastery it's easier to get around.  One does get mud on 
one's shoes.
> eval evaluates shell code. I struggle to understand what you
> don't understand. Maybe you're thinking too much or too little
> of what a "command line string" is. A "command line string"
> like:

I think at this level of my education it's enough to say that eval looks 
at it's arguments as if they were typed on the command line.  I haven't 
really 'understood' anything, I use eval ad hoc when it seems to solve 
problems.  Using it with real understanding is a future goal.


> For that string to be passed as code to the shell language
> interpreter for it to interpret.

Right, I get that:


2 /aWorking/Zsh/Source/Wk 0 $ var="echo howdy"

2 /aWorking/Zsh/Source/Wk 0 $ eval $var
howdy

> Again, tree has nothing to do with the GNU project.

I dunno, I get it all from Debian and Debian is a GNU/Linux OS so I 
don't know more than that.


Thanks for a most educational post!  I love these history lessons, they 
inform my entire outlook.  You can't understand where you are unless you 
understand where you've been.




  reply	other threads:[~2022-11-20 16:27 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-19 14:34 Ray Andrews
2022-11-19 14:43 ` Roman Perepelitsa
2022-11-19 17:02   ` Ray Andrews
2022-11-19 17:10     ` Roman Perepelitsa
2022-11-19 18:02     ` Clinton Bunch
2022-11-19 18:18       ` Roman Perepelitsa
2022-11-19 16:48 ` Stephane Chazelas
2022-11-19 19:12   ` Ray Andrews
2022-11-19 19:50     ` Lawrence Velázquez
2022-11-19 22:21       ` Ray Andrews
2022-11-20  8:55         ` Stephane Chazelas
2022-11-20 13:47           ` Ray Andrews
2022-11-20 15:08             ` Stephane Chazelas
2022-11-20 16:27               ` Ray Andrews [this message]
2022-11-20 20:16                 ` Stephane Chazelas
2022-11-20 22:31                   ` Bart Schaefer
2022-11-21  2:13                     ` Ray Andrews
2022-11-20 22:47                   ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c717fdc0-7661-3387-d0c8-3aff56d7f6c7@eastlink.ca \
    --to=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).