Braindump

Bash Single Quotes

Using single quotes inside of strings that are enclosed in single quotes

The wrong way

$ echo 'The Hitchhiker\'s Guide to the Galaxy'

This won't work, as //escaping is disabled within single quotes//.

The right way

$ echo 'The Hitchhiker'\''s Guide to the Galaxy'

You have to first end the string (to re-enable escaping), then write the single quote and then continue with your string.

$ echo 'The Hitchhiker'\''s Guide to the Galaxy'
The Hitchhiker's Guide to the Galaxy
$ # For the interpreter this looks as follows:
$ echo 'The Hitchhiker'\
> \'\
> 's Guide to the Galaxy'

Great use case

Replace \' through '' when converting MySQL scripts to Oracle SQL using sed:

sed 's/\\'\/'\'\/' foo.sql

$ # Proof / Test for the call to sed:
$ echo "SELECT * FROM books WHERE title='The Hitchhiker\\'s Guide to the Galaxy'"
SELECT * FROM books WHERE title='The Hitchhiker\'s Guide to the Galaxy'
$ echo "SELECT * FROM books WHERE title='The Hitchhiker\\'s Guide to the Galaxy'" | sed 's/\\'\''/'\'\''/'
SELECT * FROM books WHERE title='The Hitchhiker''s Guide to the Galaxy'