quotes in a bash script?

topic posted Tue, March 13, 2007 - 2:09 PM by  smoorefu
Share/Save/Bookmark
Advertisement

The one place I always get in trouble when it comes to scripting is with quoting. No matter how many times I run into it, read up on it, etc. I can't seem to retain it for some reason. (Probably exacerbated by the fact that I only write scripts once in a while.)

I want to modify a string that contains a ' by using sed. If I were to do it inside of vi, :1,$ s/'/ /g would work fine.

But if I try to do it in a script, normally one would use single quotes around it, which gives sed '/'/ /g/ filename > new.filename . This doesn't work for obvious reasons. But what is the correct way to do this?

I've tried escaping the single quote with a \, I've tried enclosing the lot with double quotes or back quotes instead, but nothing is working. (You can tell I'm flailing without a proper idea -- that's what I mean, I can't seem to retain this quoting stuff.)

Any insights would be *most* gratefully received!
Advertisement
Advertisement
  • Re: quotes in a bash script?

    Tue, March 13, 2007 - 2:15 PM

    Replying to my own message, how tacky! Upon reviewing my notes, I see that a friend of mine suggested enclosing the whole thing in double quotes and then escaping the ' with a \. However, that doesn't work under MKS Toolkit or Linux (Red Hat), which are the only flavours I have available to me at my work. (I know, I know, "get a real computer". Believe me, I would if I could.)
  • Re: quotes in a bash script?

    Tue, March 13, 2007 - 4:04 PM
    You can switch in and out of which type of quotes you're using, eg:
          's/'"'"/ /g'
    which is probably impossible to read here, but it's:
          single s slash single double single double single slash space slash g single

    What?!?? I hear you say?
    It "simply" starts in single quotes, and which you want a single quote you come out, go into double quotation to quote a single quote, then come out of double quotation, to go back into single quotes again.

    Of course this is not the best way in such a small example, but if you have an inline awk or perl script, it is often best to use single quotes, and to come out and use double quotes for shell variables or single quotes.

    I hope this makes sense?
    I hope it works with MKS tool kit
    • Re: quotes in a bash script?

      Tue, March 13, 2007 - 4:15 PM

      Thanks for the great suggestion, but unfortunately MKS Toolkit doesn't appear to like that either. It's complaining about missing the closing double quotes. (I.e. it says "missing closing doublequote doublequote doublequote")

      But! Holy Cow, the previous trick (double quote the whole thing, then escape the single quote with a \) now appears to be working. Don't know why I made a note that it didn't work before, guess I was having a blond moment.

      Thanks!!
      • Re: quotes in a bash script?

        Wed, March 14, 2007 - 4:05 AM
        Another variation that might work out is: 's/\'//', i.e. singlequote slash backslash singlequote slash slash singlequote.

        The logic is that you are using the backslash to escape the character that would otherwise tag the end of the quoted string.

Recent topics in "Unix Supremacy"

Topic Author Replies Last Post
UNIX puzzle task Lee 1 December 10, 2009
Phoenix OWASP July/August Meetings obnosis 0 July 2, 2009
FusionStorm licks my nuts. Poorly. Unsubscribed 4 March 30, 2009
happy 1.2 million! onTonca 1 January 15, 2008