Braindump

Bash Single Quotes

Braindump.BashSingleQuotes History

Show minor edits - Show changes to markup

January 17, 2010, at 02:06 PM by 83.77.90.209 -
Added lines 1-21:

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

Wrong: $ echo 'The Hitchhiker\'s Guide to the Galaxy' -- Won't work, as //within single quotes the escape is disabled//. Richtig: $ 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 get 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'