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

Re: Printing square brackets and backslashes



DervishD sent me the following 0.9K:

> >     Hi all :)
> > 
> >     $ print \[\\\]
> >     []
> >     $ /bin/echo -e \[\\\]
> >     [\]
> >     $ print -r \[\\\]
> >     [\]
> >     $ print \\
> >     \
> 
>     I can understand the first case, but I don't understand the last
> one: in the first case, what I misunderstood is that the shell quotes
> the chars, producing "[\]" and "print" prints them, interpreting some
> escape directives: "[]". But in the last case, nothing should then be
> printed, because zsh quotes the backslash and "print" gets a single
> backslash, that is, an empty escape directive that shouldn't print
> anything :? Is "print" assuming that a single backslash is not an
> empty escape directive? Is a single, isolated backslash a synonim for
> "\\"?

This is in the zsh user's guide at:

   http://zsh.sourceforge.net/Guide/zshguide03.html#l32

Look under section 3.2.1, Builtins for printing.

# -------------------------------------------------------------------- 

Secondly, those backslashes can land you in real quoting difficulties.
Normally a backslash on the command line escapes the next character ---
this is a different form of escaping to print's --- so

  print \n

doesn't produce a newline, it just prints out an `n'. So you need to
quote that. This means

  print \\

passes a single backslash to quote, and

  print \\n

or

  print '\n'

prints a newline (followed by the extra one that's usually there). To
print a real backslash, you would thus need

  print \\\\

Actually, you can get away with the two if there's nothing else after
--- print just shrugs its shoulders and outputs what it's been given ---
but that's not a good habit to get into.

-- 
Chris Johnson
cjohnson@xxxxxxxxxx
http://www.cs.utk.edu/~cjohnson



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