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 6205615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:18:22+00:00 2026-05-24T05:18:22+00:00

After hours of fighting with a script, I narrowed the problem down to the

  • 0

After hours of fighting with a script, I narrowed the problem down to the following. Running this at the command prompt (bash) to shows the issue:

bash$ echo "\n"
\n
bash$ echo "\n" | sed 's/\\n/---\\&/g'
---\\n
bash$ str=`echo "\n" | sed 's/\\n/---\\&/g'`
bash$ echo $str
\n
bash$ 

I don’t understand why that str=... command is not equivalent to str= output of command in ....

Why is it not equivalent to str=”—\n” ?

I must be missing something fundamental here.

EDIT: In response to comments:

$ /bin/bash --version
GNU bash, version 4.2.8(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

also, the solution isn’t as simple as the answer stating only the first part of the pipe is returned in a backtick.
Notice:

bash$ str=`echo "abc" | sed 's/a/---&/g'`
bash$ echo $str
---abc
bash$

So somehow it is a combination of things including \ characters that are causing the problem. I can’t quite figure out the behavior here.

  • 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-05-24T05:18:23+00:00Added an answer on May 24, 2026 at 5:18 am

    Other answer is probably correct about some quoting being dropped. The trick that I’ve learned lately is to use $() instead of backticks.

    str=$(echo "\n" | sed 's/\\n/---\\&/g')
    

    That works as expected.


    Update

    Here is how it seems to work. Consider this example first:

    $ echo "\\n"
    \n
    $ echo '\\n'
    \\n
    

    In shell there is strong and weak quoting. Weak quoting is represented via ” (double quote) and strong quoting is represented via ‘ (single quote). Weak quoting does not prevent escaping while strong will prevent it. And back slash has the meaning of preserving next character except newline. So “\\n” is actually equal to two characters: \ and n because first slash is escaped. Which are printed. Strong quotes, on the other hand, will not do any escaping and ‘\\n’ becomes three characters. \, \ and n.

    One other property of weak quotes is that they prevent strong quotes from taking effect and make them be interpreted literally.

    So now, consider example similar to your sed example:

    $ echo `echo '\\n'`
    \n
    

    So what happens here? back ticks are processed as weak quotes, that is strong quotes inside stop doing any quoting. So ‘\\n’ gets interpreted as ‘\n’ since \ gets escaped. As a result, command that gets executed is echo '\n' and not echo '\\\\n'. And that’s exactly what happened with you sed.

    Awesome question, really made me think and research it.

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

Sidebar

Related Questions

This is my first bash at using extJS, and after a few hours of
I originally thought this was a WordPress issue - but realize after hours of
This problem seems trivial but I'm at my wits end after hours of reading.
I'm embarrassed by the 'basicness' of this question, but after wasted hours, here goes.
Update: After playing around with this for a few hours, went with a multi-query
I have an assignment and after hours of pondering and fighting off my illness
I can't figure out what it is going on here...after hours... This is the
There are a few posts about this, but after hours of searching I still
Struggling still with this after hours or research.. I have a simple helper class
I've seen this question asked over at the Raphael Google Groups, but after hours

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.