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

Re: trap question



On Sat, Nov 26, 2022 at 4:04 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> I'd put it the other way: trap and eval are both special cases, no?  Surely the general rule is that nothing is ever expanded within single quotes?

There are no exceptions to this rule. The content enclosed in single
quotes never undergoes parameter expansion, command substitution or
arithmetic expansion: $ stays $. Now, what you are going to do with
the *value* that results from the evaluation of a single-quoted
literal is up to you. You can certainly decide to perform those
expansions.

    foo='$(echo hello)'
    some-command "$foo"

The first line doesn't expand $(echo hello) but the second command
might. Maybe this command passes the argument to trap, eval, print -P,
${(e)1}, etc. The command doesn't care whether the argument was
created by enclosing something in single quotes and in fact has no way
of knowing this. It only sees the value but not the providence of it.

    foo="\$(echo hello)"
    some-command "$foo"

This second snippet is exactly identical to the first. The command
will receive the same argument and won't be able to tell the
difference.

Roman.




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