zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: Zsh Users <zsh-users@zsh.org>
Subject: syntactic question
Date: Fri, 21 Nov 2014 12:23:29 -0800	[thread overview]
Message-ID: <546F9F41.8040608@eastlink.ca> (raw)

All,

Running this script:

    test="one\ttwo"

    echo  1 ${test//'\'/\\}
    echo "2 ${test//'\'/\\}"
    echo  3 ${test//\\/\\}
    echo "4 ${test//\\/\\}"
    echo  5 ${test//'\'/'\'}
    echo "6 ${test//'\'/'\'}"
    echo "7 ${test//'\'/'\\'}"
    echo "8 ${test//'\'/'\\\\'}"
    echo "9 ${test//'\\'/'\\'}"

... I get this output:

    1 one    two
    2 one    two
    3 one    two
    4 one    two
    5 one    two
    6 one'\'ttwo
    7 one'\'ttwo
    8 one'\'ttwo
    9 one    two


Lines one thru five are no puzzle, they're just different ways
of saying the same thing: we're replacing the backslash character
with itself (as an exercise). But at line six the rules change.
Now the outer quotes seem to be meddling inside the substitution,
and the single-quoted backslash output becomes literal: '\'
but the input syntax is unchanged.

I've always thought that all parsing systems work from the 'inside out'
which is to say that (as with math) you 'do' the deepest parenthesis
first, and then work outward. In the same spirit, I'm expecting
the substitution to do what it does following it's own rules and
nevermind any quotations or anything else 'further out'. But if
zsh does permit the outer quotes to change what happens inside
the substitution, then why is it not equal in it's effect on the
input string?

I'm puzzled by the fact that lines six, seven and eight produce the
same output, and there again there is no symmetry between the input
and the output strings, since, in line nine, doubling the input
backslash doesn't work, whereas it does work in the output string (seven).
It seems 'obvious' to me that all of the above, except seven and eight,
are saying 'replace a backslash with itself'. Six should work the same
as one thru five, and if seven works, then so should nine. Why line
eight works, I'm not sure I even want to know ;-)

In practice there's no huge problem tho, since the 'all backslash'
forms always work the same way. Is this understandable for a mere
mortal?


             reply	other threads:[~2014-11-21 20:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-21 20:23 Ray Andrews [this message]
2014-11-21 21:29 ` Bart Schaefer
2014-11-21 23:46   ` Ray Andrews
2014-11-22  0:00     ` Bart Schaefer
2014-11-22  1:40       ` Ray Andrews
2014-11-21 21:42 ` Kurtis Rader
2014-11-21 23:45   ` Bart Schaefer
2014-11-22  4:39     ` 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=546F9F41.8040608@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).