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.
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.
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.)
’
with the sequencecompose
,'
,>
. The others have analogous compose key sequences. – LeopardShark Jun 04 '22 at 11:29:set hlsearch
followed by/[^ -~]
: vim then highlights every non-ASCII character in the buffer. – Edgar Bonet Jun 04 '22 at 12:33