Muffinresearch Labs by Stuart Colville

BASH: Single-quotes inside of single-quoted strings | 30 Comments

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

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

  • Ryan

    Thanks for the tip! It worked pefectly!

  • http://robots.org.uk Sam Morris

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

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

  • Poor Yorick

    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

  • Isaac

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

  • Arif

    Worked perfectly! Digging it!

  • Matt

    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!

  • bruce

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

  • Shaun Krislock

    Thanks so much for posting this easy to understand solution

  • Kevin

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

  • Pingback: Chris Rodgers » Blog Archive » Escape single quotes in bash

  • mm22

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

  • cos

    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 :)

  • jas

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

  • rob

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

  • Will

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

  • baynaa

    Thanks! worked for me like a charm.

  • http://shortreckonings.com Mikael

    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!

  • dan

    You are a crazy command-line genius.

  • http://devrandom.postr.hu/ karatedog

    Thanks, you saved me at least 4 hours!

  • Andrew Banks

    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

  • Googie

    Thanks a ton! Works like a charm..

  • Ryan

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

  • http://moonpixel.com moonpixel

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

  • rsingh

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

  • Nikhil S

    same here.. 

    Stuart, keep up the good work !

  • Pingback: sed get everything between two charcters

  • taylor

    thanks!

  • Linux user

    August 2012 and still it’s helpfull

  • mm

    god bless you.

  • grateful

    Dude you are amazing – thankyou very much!

Insert a tab character in vim when expand tabs is on|(0)

I have vim set-up to use spaces in place of tabs. Sometimes you need to use an actual tab e.g. editing a Makefile. Now whilst it’s possible to change settings so that tabs are used for specific files, a quick tip to remember is to simply type in insert mode:

Ctrl+v tab

That is Ctrl and “V” and hit the tab key, et voila you’ve entered an actual tab.

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

Photos on Flickr

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