Braindump

Bash Single Quotes

Braindump.BashSingleQuotes History

Hide minor edits - Show changes to markup

January 17, 2010, at 02:16 PM by 83.77.90.209 -
Changed lines 3-4 from:

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.

to:

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.

Changed lines 23-28 from:

Great use case: Replace \' through when converting MySQL scripts to Oracle SQL using sed: sed 's/\\'\/'\'\''/' foo.sql

to:

Great use case

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

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

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'