5 SC1011
wileyhy edited this page 2024-10-06 00:18:46 -07:00

This apostrophe terminated the single quoted string!

Problematic code:

echo 'Nothing so needs reforming as other peoples' habits.'

Correct code:

echo 'Nothing so needs reforming as other peoples'\'' habits.'

or

echo "Nothing so needs reforming as other peoples' habits."

Rationale:

When writing a string in single-quotes, you have to make sure that any apostrophes in the text don't accidentally terminate the single-quoted string prematurely.

Escape them properly (see the correct code) or switch quotes to avoid the problem.

Additional options:

echo '...peoples\ habits.'
...peoples\ habits.
$ echo $'...peoples\x27 habits.'
...peoples' habits.

Exceptions:

None.

https://www.gnu.org/software/bash/manual/html_node/Quoting.html