zsh-users
 help / color / mirror / code / Atom feed
* Why this doesn't work?
@ 2005-03-12 10:14 DervishD
  2005-03-12 20:49 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: DervishD @ 2005-03-12 10:14 UTC (permalink / raw)
  To: Zsh Users

    Hi all :))

    I have a problem with quoting and 'eval'. I know that, withing
double quotes, the '\' is a quoting char only if followed by '\','$',
'"', ''' and newline, otherwise is a literal backslash. This allow
for this:

    $ printf -- "Hello\n"
    Hello<newline>

    In that expression, the '\' is a literal '\', no problem. The
problema arises when:

    $ eval printf -- "Hello\\n"
    Hello$
    $ eval printf -- "Hello\\\n"
    Hello<newline>

    I mean, no newline character is output when using just two
backslashes, and I don't understand that. In the first case, with two
backslashes, I think that the shell interprets it as 'Hello' plus
'\\' (that is, a backslash) and a 'n', so the shell runs a command
like: 'printf -- "Hello\n"'. Obviously, it doesn't.

    The second example, as I understand it, should expand to:

    printf -- "Hello\\n"

    that is, "Hello" plus "\\" plus "\n". If the double-quotes rule
is not followed in 'eval' commands, it should expand to "Hello" plus
"\\" resulting in one "\" and "\n" resulting in "n", but then in the
example with just two backslashes it should expand to "Hello" plus
"\\" resulting in "\" and a "n", that is, the same. And obviously is
not. What am I missing?

    I always use three backslases in 'eval' commands because sometime
in the past I learned why, but I've forgotten, sorry O:))) Anyone
could help?

    Thanks a lot in advance :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.dervishd.net & http://www.pleyades.net/
It's my PC and I'll cry if I want to...


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Why this doesn't work?
  2005-03-12 10:14 Why this doesn't work? DervishD
@ 2005-03-12 20:49 ` Bart Schaefer
  2005-03-13  9:39   ` Why this expansion " DervishD
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2005-03-12 20:49 UTC (permalink / raw)
  To: Zsh Users

On Mar 12, 11:14am, DervishD wrote:
} Subject: Why this doesn't work?

As long as I'm passing out netiquette lessons, I might as well say
that it would be nice if the word "this" in that Subject had been
replaced by a slightly more descriptive phrase.

}     $ eval printf -- "Hello\\n"
}     Hello$

If this is really what you're seeing, then something is indeed wrong.
What you should see is "Hellon", not "Hello".

}     $ eval printf -- "Hello\\\n"
}     Hello<newline>
} 
}     I mean, no newline character is output when using just two
} backslashes, and I don't understand that.

Remember that when you use "eval" the command line is actually parsed
twice.  So
	eval printf -- "Hello\\n"
is equivalent to
	printf -- Hello\n

The first parse removes the quotes and one of the backslashes, and the
second parse removes the remaining backslash.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Why this expansion doesn't work?
  2005-03-12 20:49 ` Bart Schaefer
@ 2005-03-13  9:39   ` DervishD
  2005-03-13 17:16     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: DervishD @ 2005-03-13  9:39 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> On Mar 12, 11:14am, DervishD wrote:
> } Subject: Why this doesn't work?
> As long as I'm passing out netiquette lessons, I might as well say
> that it would be nice if the word "this" in that Subject had been
> replaced by a slightly more descriptive phrase.

    Yes, you're right, but I couldn't think of a more apropriate
subject at the time, sorry O:)) Next time I'll use a better and more
descriptive subject.
 
> }     $ eval printf -- "Hello\\n"
> }     Hello$
> If this is really what you're seeing, then something is indeed wrong.
> What you should see is "Hellon", not "Hello".

    That's weird :(( I still have open the console where I saw that,
but in a new virtual console that doesn't happen and yes, I see
'Hellon' and not 'Hello'. I don't know why, because the options are
the same in both terminals (I've double checked that) :???

    On every new terminal I see 'Hellon'.
 
> }     $ eval printf -- "Hello\\\n"
> }     Hello<newline>
> }     I mean, no newline character is output when using just two
> } backslashes, and I don't understand that.
> Remember that when you use "eval" the command line is actually parsed
> twice.  So
> 	eval printf -- "Hello\\n"
> is equivalent to
> 	printf -- Hello\n
> 
> The first parse removes the quotes and one of the backslashes, and the
> second parse removes the remaining backslash.

    So I must use something like:

    eval printf -- \"Hello\\n\"

    I forgot that the double quotes were removed :((( Is there any
way of seeing how a command line is parsed *just before* being
executed by the shell? That would avoid the stupidity from the user
(that is, me) you can see above O:) I've tried the eval command with
the double quotes quoted and works perfectly ;)

    Thanks a lot for answering, Bart, and my excuses for the poor
subject O:)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.dervishd.net & http://www.pleyades.net/
It's my PC and I'll cry if I want to...


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Why this expansion doesn't work?
  2005-03-13  9:39   ` Why this expansion " DervishD
@ 2005-03-13 17:16     ` Bart Schaefer
  2005-03-14 10:50       ` DervishD
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2005-03-13 17:16 UTC (permalink / raw)
  To: Zsh Users

On Mar 13, 10:39am, DervishD wrote:
}
} > What you should see is "Hellon", not "Hello".
} 
}     That's weird :(( I still have open the console where I saw that,
} but in a new virtual console that doesn't happen

Cosmic rays.

} > The first parse removes the quotes and one of the backslashes, and the
} > second parse removes the remaining backslash.
} 
}     So I must use something like:
} 
}     eval printf -- \"Hello\\n\"

Yes, except of course that means something considerably different when
there are spaces or metacharacters in the (no longer double-quoted on
the first parse) string.  More likely you want

	eval printf -- \""Hello\\n"\"

}     I forgot that the double quotes were removed :((( Is there any
} way of seeing how a command line is parsed *just before* being
} executed by the shell?

setopt xtrace

(or the equivalent "set -x").  Note that xtrace in recent versions of
zsh re-quotes the output so that it's suitable for cut-and-paste; old
versions (4.0.x and before) show the fully-unquoted result, but there
it's difficult to see where the shell split strings into words.

So e.g.

zagzig% set -x
zagzig% eval printf -- \""Hello\\n"\"
+Src/zsh:2> eval printf -- '"Hello\n"'
+(eval):1> printf -- 'Hello\n'
Hello
zagzig% 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Why this expansion doesn't work?
  2005-03-13 17:16     ` Bart Schaefer
@ 2005-03-14 10:50       ` DervishD
  0 siblings, 0 replies; 5+ messages in thread
From: DervishD @ 2005-03-14 10:50 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> } > What you should see is "Hellon", not "Hello".
> }     That's weird :(( I still have open the console where I saw that,
> } but in a new virtual console that doesn't happen
> Cosmic rays.

    I'm seriouly thinking about that XDDD Probably the cause have
been the cosmic rays... hitting my brain. Or my poor eye quality, who
knows...
 
> } > The first parse removes the quotes and one of the backslashes, and the
> } > second parse removes the remaining backslash.
> }     So I must use something like:
> }     eval printf -- \"Hello\\n\"
> Yes, except of course that means something considerably different when
> there are spaces or metacharacters in the (no longer double-quoted on
> the first parse) string.  More likely you want
> 
> 	eval printf -- \""Hello\\n"\"

    Errr... yes. I'll fix it right now. I'm using this in a script
and in a couple of places I think I'll need double quotes (in
addition to the quoted ones...).
 
> }     I forgot that the double quotes were removed :((( Is there any
> } way of seeing how a command line is parsed *just before* being
> } executed by the shell?
> setopt xtrace

    I forgot. I use xtrace to see the progress in scripts, but I
never used it for seeing expansions O:) Thanks a lot for the advice.
 
> (or the equivalent "set -x").  Note that xtrace in recent versions of
> zsh re-quotes the output so that it's suitable for cut-and-paste; old
> versions (4.0.x and before) show the fully-unquoted result, but there
> it's difficult to see where the shell split strings into words.

    I'm using 4.2.x now.

> So e.g.
> 
> zagzig% set -x
> zagzig% eval printf -- \""Hello\\n"\"
> +Src/zsh:2> eval printf -- '"Hello\n"'
> +(eval):1> printf -- 'Hello\n'
> Hello
> zagzig% 

    Nice :)) Thanks a lot, Bart. You're fantastic :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.dervishd.net & http://www.pleyades.net/
It's my PC and I'll cry if I want to...


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-03-14 10:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-12 10:14 Why this doesn't work? DervishD
2005-03-12 20:49 ` Bart Schaefer
2005-03-13  9:39   ` Why this expansion " DervishD
2005-03-13 17:16     ` Bart Schaefer
2005-03-14 10:50       ` DervishD

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).