Bash Single Quotes
Braindump.BashSingleQuotes History
Hide minor edits - Show changes to markup
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.
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.
Great use case: Replace \' through when converting MySQL scripts to Oracle SQL using sed: sed 's/\\'\/'\'\''/' foo.sql
Great use case
Replace \' through '' when converting MySQL scripts to Oracle SQL using sed:
sed 's/\\'\/'\'\/' foo.sql
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'