qweruiop
qweruiop

Reputation: 3266

Confusing Shell Quotation

I'm not a newbie to shell, but still got confused with some not so complex quotation problems. I guess there must be something misunderstood.

a: echo 'Don\'t quote me // Don    quote me
b: echo Don'\t' quote me // Don    quote me
c: echo Don\t quote me   // Dont quote me
d: echo Don"\t" qoute me // Don    quote me

Above three quotations go quite against my intuition. Doesn't single quote '...' literally returns what is quoted? What I thought is..

For a: in single quoted 'Don\', \ is nothing but a common character. So a) should be Don\t quote me.

For b: like a), '\t' suppressed the special meaning of \t, so I thought b) should be Don\t quote me too.

For c: I understand why c works, but don't understand the diff between a&b and c.

For d: no difference between ' and "?

Probably I misunderstand how shell parse and execute the line of command..

Problem solved by using /bin/echo instead of (built-in)echo on Mac. Latter one will interpret backslash.

Upvotes: 3

Views: 477

Answers (2)

Saddam Abu Ghaida
Saddam Abu Ghaida

Reputation: 6749

As per bash

  1. the first one should return Don\t quote me
  2. the second should return like the first one
  3. the third should return Dont quote me
  4. the last one should return Don\t qoute me

Why:

  1. first one you scaped the don\t by putting it inside single quotes
  2. you scaped only the \t
  3. there is no scaping because \t means print the character after \ as is
  4. double quote doesnt scape scape characters

Upvotes: 3

Krzysztof Kosiński
Krzysztof Kosiński

Reputation: 4335

Your understanding of shell quoting is correct, but it appears that echo on OSX is a shell builtin which interprets backslash escapes. This behavior can be turned off by executing shopt -u xpg_echo.

See here for more information: How can I escape shell arguments in AppleScript?

Upvotes: 3

Related Questions