Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9042455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:29:45+00:00 2026-06-16T10:29:45+00:00

I’m having a problem with sed and single quotes I have a string like

  • 0

I’m having a problem with sed and single quotes

I have a string like this :

-cmd af -i 3 -a

I want to change this string to

-cmd 'af -i 3 -a'

Now I’m using this :

the string is stored in buff variable

buff=$(echo $buff | sed -r "s/cmd /CMD '/g")"'"

it echoes ok -cmd 'af -i 3 -a'

I then call a function with buff as a parameter and while executing the script with ksh -x I can see the call is NOK

+ buff=$'-CMD \'af -i 3 -a \''
+ echo -CMD $'\'af' -i 3 -a $'\''
-CMD 'af -i 3 -a' #this is the echo
+ functionTest -CMD $'\'af' -i 3 -a $'\''

As a result it will always give me

-CMD $'\'af' -i 3 -a $'\''

So basically it seems my single quotes are interpreted as $'\''

I have no idea why, I tried a lot of things such as escaping chars but it gives me the exact same result, even with \x27

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:29:46+00:00Added an answer on June 16, 2026 at 10:29 am

    How are you seeing the odd-ball result?

    What you’ve got is OK in the raw (sed on Mac OS X does not support -r, but the regex used doesn’t need the non-standard GNU extension option -r anyway):

    $ buff1="-cmd af -i 3 -a"
    $ buff2=$(echo $buff1 | sed -e "s/cmd /CMD '/g")"'"
    $ echo $buff2
    -CMD 'af -i 3 -a'
    $
    

    I suspect your problem is not in the transform, but in the way you’re viewing the result.


    ksh test

    Osiris JL: cat so14043243.sh
    buff1="-cmd af -i 3 -a"
    buff2=$(echo $buff1 | sed -e "s/cmd /CMD '/g")"'"
    echo $buff2
    echo "$buff2"
    Osiris JL: ksh -x so14043243.sh
    + buff1='-cmd af -i 3 -a'
    + sed -e $'s/cmd /CMD \'/g'
    + echo -cmd af -i 3 -a
    + buff2=$'-CMD \'af -i 3 -a\''
    + echo -CMD $'\'af' -i 3 $'-a\''
    -CMD 'af -i 3 -a'
    + echo $'-CMD \'af -i 3 -a\''
    -CMD 'af -i 3 -a'
    Osiris JL: 
    

    The output is correct; the trace is slightly confusing but the same as what you show.

    There is a good reason for the extra effort that the -x output goes to; it disambiguates the output so that you can determine exactly what is what, if you can read it well enough. Also, you could copy’n’paste the line after the + and run it and get exactly the same result again. The old Bourne shell had a -x option which did not do character mapping like this, and it could lead to confusion, and you certainly could not reliably copy’n’paste the trace output to execute the command again.

    The key point is that the result — what is echoed — is what you want and expect, so you’re fretting over nothing (as it happens). But I agree, it can be confusing at first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.