Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Block comments ala Ray



2021-02-12 13:16:28 -0500, Lawrence Velázquez:
[...]
> This was me, addressing someone with shaky shell fundamentals who
> was aliasing the here-document idiom and expecting it to behave
> identically to real comments (discarded during lexing, no further
> impact on the script).
[...]

Thanks all for the clarifications.

About "discarding during lexing", I did find these btw:

$ ksh93 -c $'f() {\n# whatever\necho  x\n}; typeset -f f'
f() {
# whatever
echo  x
}

It appears as if they're not discarded(though it looks like it's
rather that ksh stores the raw text content (probably only for
the "typeset -f" output) in addition to "compiling"/"wordcoding"
or however you want to call it (which probably still discards
comments)).

But see also (as that construct was mentioned in that thread):

$ zsh -c 'f() { echo `echo x # comment`; }; typeset -f f; f'
f () {
        echo `echo x # comment`
}
x
$ mksh -c 'f() { echo `echo x # comment`; }; typeset -f f; f'
f() {
        \echo $(echo x # comment)
}
x
$ bash -c 'f() { echo `echo x # comment`; }; typeset -f f; f'
f ()
{
    echo `echo x # comment`
}
x

as the parsing of the inside of `...` is deferred in all shells
except ash-based ones; see $shell -c ':||echo `for`'. A bit like
in eval 'echo x # comment'.

It's also deferred with the $(...) form in bash (but bash only
AFAICT):

$ bash -c $'f() { echo $(echo x # comment\n); }; typeset -f f'
f ()
{
    echo $(echo x # comment
)
}

That newline is necessary in all shells though there, and that
makes the replacing of `...` with $(...) in the typeset -f
output by mksh above incorrect:

~$ mksh -c 'f() { echo `echo x # comment`; }; typeset -f f' | mksh
mksh: <stdin>[3]: syntax error: unexpected '}'

-- 
Stephane




Messages sorted by: Reverse Date, Date, Thread, Author