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

Re: triviality regarding $# counts



On Sat, Apr 13, 2024 at 11:08 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> But I'm still not happy with the dollars.  I had thought that "  $'...'  "  was grammatical but we have " ' ' "  -- meaning an empty line

There are three* forms of quoting:

1) "X" -- expand variable references and other substitutions in X, but
do not split on $IFS **
2) 'X' -- do not do any expansions, X is literal
3) $'X' -- interpret \n \t \e etc., but everything else is literal, as
is the final result

This is all explained in the manual section "Quoting".  "typeset -p"
will output either no quoting at all, or #2, or #3, depending on which
is most appropriate to the value of the parameter, such that the
entire output is printable without the possibility of events like
terminals interpreting escape sequences.  If the parameter is an
array, "typeset -p" may output some elements in #2 form and others in
#3 form depending on the value of each element.

Note that because #3 is quoting, not substitution, it does not expand
inside #1.  Conversely, backticks `X` only look like quoting, they are
actually substitution equivalent to $(X) except without nesting, so
they do execute a command when appearing inside double quotes (#1).

* Four if you count using a backslash to protect a single following character.
** $@ and $ary[@] and ${(@)ary} get special handling, their array-ness
is preserved with each element quoted separately.

Let's look at your "redline" function:

redline () { echo -e "$red$@$nrm" }

The special handling of $@ means that for example:

redline a b c

is interpreted as

echo -e "$red$1" "$2" "$3$nrm"

So you probably should have written

redline () { echo -e "$red$*$nrm" }

because $* is the same as $@ except without that special
interpretation inside double quotes.




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