Muffinresearch Labs by Stuart Colville

BASH: Single-quotes inside of single-quoted strings | Comments (24)

Posted in Code, Linux/Unix on 30th January 2007, 11:38 pm by Stuart

Using single quotes in BASH ensures that the shell doesn’t expand the contents of the quoted string and this is useful most of the time. However if you want to use single quotes within a single quoted string things don’t work out as you might expect.

For example using the following simple PHP snippet:

<?php
echo '<p>this is a string</p>';
?>

If I want to use sed to match some text with a single quotes in it, I will run into trouble if I run:

sed -e s/'<p>/'<p class="test">/g

This will output:

/bin/bash: line 1: p: No such file or directory

This is because the unquoted string will be expanded and BASH will think that < is a redirection.

Alternatively if I run (on the same snippet):

sed -e 's/\'<p>/\'<p class="test">/g'

I will get:

/bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/bin/bash: -c: line 2: syntax error: unexpected end of file

This doesn’t work because the escaped single-quotes (\') are not expanded and are therefore treated literally.

To single quotes work you need to break out of the single quoted string then escape your single quote. Like so:

sed -e 's/'\''<p>/'\''<p class="test">/g'

Because \' is not inside of single quotes the single-quote is properly escaped and the output is as we’d expect:

<?php
echo '<p class="test">this is a string</p>';
?>

In conclusion, the title of this post is a bit of a misnomer. You actually can’t put single quotes inside of a single-quoted string. However breaking out allows us to get to where we want to be.

Post Tools

Comments: Add yours

1. On March 22nd, 2007 at 8:38 pm Ryan said:

Thanks for the tip! It worked pefectly!

2. On May 16th, 2007 at 8:14 pm Sam Morris said:

You can also use ANSI C style quoting. This is not documented in bash’s man page, annoyingly.

$ echo $'foo\'bar'
foo'bar

3. On October 9th, 2007 at 3:44 pm Poor Yorick said:

It is documented in my version of the man page, dated 2002 July 15, in the section called QUOTING:

Words of the form $'string' are treated specially…

\' single quote

4. On January 6th, 2008 at 3:48 pm Isaac said:

Bingo! It works! Thanks.
I needed it to use awk inside an alias.

5. On July 31st, 2008 at 9:25 am Arif said:

Worked perfectly! Digging it!

6. On August 18th, 2008 at 4:20 pm Matt said:

So if you have a string that you’re cleaning up, say in python:

str.replace(“‘”,”‘\\””)

first ‘ – end single quoted string
\\ – literal backslash
‘ – single quote to be backslashed
‘ – resume single quoted string

Thanks!

7. On February 11th, 2009 at 9:36 pm bruce said:

thanks. also for bash alias. \’ didn’t work for me. suspect specialized parse around $x variables.

8. On June 10th, 2009 at 12:27 am Shaun Krislock said:

Thanks so much for posting this easy to understand solution

9. On August 25th, 2009 at 2:54 am Kevin said:

Wow! This is exactly the problem I had, and a perfect solution. Great post!

10. On September 14th, 2009 at 8:52 pm Chris Rodgers » Blog Archive » Escape single quotes in bash said:

[...] helpful instructions on Stuart Colville’s blog explain how to do [...]

11. On September 23rd, 2009 at 4:48 pm mm22 said:

This was exactly what I needed to get my script working the way I expected. Many thanks!
mm22

12. On January 23rd, 2010 at 12:12 pm cos said:

Thanks, i used it for generating a web page where i had problems with enclosed “”” part. It is also a logic “rule” so, easy to remember :)

13. On June 28th, 2010 at 2:12 pm jas said:

Thanks! I needed this for generating quoted strings in sql statements using awk from the command line.

14. On August 26th, 2010 at 8:53 am rob said:

Thanks Stuart and Sam! This solves a problem I’ve been frustrated at for a couple of years now.

15. On December 26th, 2010 at 11:11 pm Will said:

You saved me a night of work troubleshooting. Thanks a bunch!

16. On December 28th, 2010 at 8:08 am baynaa said:

Thanks! worked for me like a charm.

17. On May 25th, 2011 at 2:47 pm Mikael said:

Awesome, the quote-backslash-quote-quote trick saved my day!
I am applying it to command line perl calls and it allows to have single quotes within the single quote of the -e ‘…’ part. Kudos!

18. On September 28th, 2011 at 5:28 pm dan said:

You are a crazy command-line genius.

19. On November 14th, 2011 at 4:48 pm karatedog said:

Thanks, you saved me at least 4 hours!

20. On November 16th, 2011 at 5:58 pm Andrew Banks said:

Thank you, I was beginning to think my attempt was a lost cause, because others had said that working with single ticks and the shell were very difficult, and they didn’t have a solution

21. On December 2nd, 2011 at 7:51 am Googie said:

Thanks a ton! Works like a charm..

22. On December 6th, 2011 at 3:31 am Ryan said:

December 2011 and you’re still helping people. Thanks :)

23. On December 14th, 2011 at 8:23 am moonpixel said:

many thanks for this! I couldn’t get the single quotes right up until I saw this post…

24. On February 1st, 2012 at 11:25 pm rsingh said:

Thanks for “quote-backslash-quote-quote trick”







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



GNU screen: open tab in current working directory|(1)

A nice trick for having screen open a new tab in the same directory as the one you’re currently in. To use it add it to your .screenrc

# Open new window in current dir.
bind c stuff "screen -X chdir \$PWD;screen^M"
bind ^c stuff "screen -X chdir \$PWD;screen^M"

Hat tip: mteckert on SuperUser.com

Ubuntu: add-apt-repository: command not found|(2)

When you’re using a minimal Ubuntu install if you find the ‘add-apt-repository’ command is missing (it’s useful for adding PPAs and other repositories), then simply run:

sudo apt-get install python-software-properties

Photos on Flickr

© Copyright 2004-12 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.529s.