8

Somehow curly quotes got into my code and I'm getting unexpected behavior

#!/bin/sh

if [ foo = ‘foo’ ]; then
   echo yes
else
   echo no
fi

I would expect this to echo yes but instead it echoes no.

1 Answer 1

22

Curly quotes, or “smart quotes” are used in typography and "straight quotes" will often be automatically changed to curly quotes by various word processing programs. They are sometimes called "smart quotes" because they somewhat intelligently determine which direction the quoted text is in and curl towards it.

Here is a table of the various quotes:

Character Desc Windows/Unix macOS HTML
' straight single quote ' ' '
" straight double quote " " "
opening single quote alt + 0145 option + ] ‘
closing single quote alt + 0146 option + shift + ] ’
opening double quote alt + 0147 option + [ “
closing double quote alt + 0148 option + shift + [ ”

In programming languages however, these quotes are not interchangeable. Straight quotes are often special characters that perform specific functions within the language while the similar looking curly quotes are not recognized as special. Instead, they may produce errors, or they may be treated as ordinary characters somewhat similarly to letters.

So in the code example in this question the curly quotes are being treated as their literal characters and not just shell escape characters and therefore 'foo' does not match '‘foo’'

To prevent this issue you should only edit your code in programs designed for code editing, and take care copying code from any word processing application (LibreOffice Word, Wordpad, email, etc), PDF documents or online code sources.

The visual distinction between curly quotes and straight quotes may be slight, but it's usually possible to tell the difference. Curly quotes often show slanted, or resemble a miniature 6 or 9, depending on the font.

Programs like od can also be used to tell the difference between the regular ASCII straight quotes, and the Unicode curly quotes. E.g. the following text file has “foo” with curly quotes on one line, and "foo" with straight quotes on the other, and in an UTF-8 locale, the output of od -c on both macOS and GNU/Linux shows the curly quotes taking multiple bytes while the straight quotes take only one:

mac$ od -c test.txt
0000000    “  **  **   f   o   o   ”  **  **  \n   "   f   o   o   "  \n
0000020

gnu$ od -c test.txt
0000000 342 200 234   f   o   o 342 200 235  \n   "   f   o   o   "  \n
0000020

(In the upper one, the macOS od shows the full character at the first position, and marks the rest of the bytes of the character with **, while in the lower, GNU od shows the values of all bytes in the characters, in octal.)

7
  • 12
    Take care copying from online sources as well, I've seen more than a few with curly quotes in online code samples.
    – Kevin
    Jun 3, 2022 at 3:00
  • macOS has a setting (Preferences-->Keyboard-->Text) for turning on or off what they call "smart quotes" (and dashes). This may affect at least some applications on that system.
    – Kusalananda
    Jun 3, 2022 at 13:37
  • Two additional notes: Some languages actually do accept smart quotes (though I can’t name any off the top of my head), and some keyboard layouts have other ways to enter them (for example, a typical US International layout allows AltGr+9 for an opening single smart quote, and AltGr+0 for the corresponding closing quote). Jun 3, 2022 at 13:47
  • 13
    curly quotes are the characters, smart quotes is the editor feature that automatically replaces straight quotes with curlies.
    – Barmar
    Jun 3, 2022 at 13:47
  • 1
    On Linux, you can enter with the sequence compose, ', >. The others have analogous compose key sequences. Jun 4, 2022 at 11:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .