Muffinresearch Labs by Stuart Colville

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

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!







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>



Inspiring a Sense of Ownership|(0)

Former colleague Mike West talks about how inspiring a team’s sense of ownership around a project is the key to great things happening: http://mikewest.org/2008/11/the-inspiration-of-ownership. Quality stuff.

VMware Server: Convert Fixed Disk-images to Growable|(0)

Quick tip if you ever want to convert from a fixed disk image to an expandable one then the following command should do it:

sudo vmware-vdiskmanager -r source.vmdk -t 0 expandable.vmdk

Just replace “source” and “expandable” with your disk image file names. For more on what vmware-vdiskmanager can do for you type vmware-vdiskmanager -h

Photos on Flickr

© Copyright 2004-08 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by 1&1 This page was baked in 0.762s.