-1

I am trying to append a new line using sed, it works only when I add the new line like this: \\\n:

echo "sometextutf8_filesystemmmmm" | sed -r "/utf8_filesystem/a \\\n# Passive mode"

the output:

sometextutf8_filesystemmmmm

# Passive mode

One or two back slashes do not work! With one \n or two \\n back slashes I just get this output:

sometextutf8_filesystemmmmm
n# Passive mode

without any new line.

Even though, it works properly without having three backslashes with substitution:

echo "sometextutf8_filesystemmmmm" | sed -r "s/utf8_filesystem/\n# Passive mode/"

Output:

sometext
# Passive modemmmm

Could some one explain that behavior?

  • @anubhava: sed (GNU sed) 4.2.2 – Mohammed Noureldin Sep 12 '17 at 11:21
  • Then sed -r '/utf8_filesystem/a\\n# Passive mode' should also work. – anubhava Sep 12 '17 at 11:35
  • @anubhava, yes I already tried that. Actually what I want is an explanation. Why do I need three or two back slashes? what am I trying to escape? – Mohammed Noureldin Sep 12 '17 at 11:43
  • If you want to avoid double escaping then use: sed -r 's/utf8_filesystem/&\n\n# Passive mode/' – anubhava Sep 12 '17 at 11:48
  • 1
0

I can't reproduce your problem using bash.

$ echo aaa | sed -r 's/aaa/AAA \n#Passive mode/'
AAA
#Passive mode

$ myprog=sed

$ echo aaa | $myprog -r "s/aaa/AAA \n#Passive mode/"
AAA
#Passive mode

so I suspect there is more to this than is stated in the question.


It's probably because

1) you probably have some complicated set of scripts and/or aliases and/or environment variables that cause your arguments to be evaluated multiple times and

2) you are using the wrong type of quotes. When you use "Params" the shell will process escapes converting \\ to \ .

You may be happier using single quotes: 'params'

compare

$ echo "s/aaa/AAA \\\n#BBB/"
s/aaa/AAA \\n#BBB/

$ echo 's/aaa/AAA \\\n#BBB/'
s/aaa/AAA \\\n#BBB/
  • It is not the wrong quotes, in my real situation, I am passing a parameter to the command $some_thing, therefore I need the double quotes. But I still do not understand why I need \\n, normally when I want to add new line, I simply use \n. – Mohammed Noureldin Sep 12 '17 at 12:05
  • anyway, if I use your case with single quotes: '\\n', why do I need for two back slashes? why not only one? – Mohammed Noureldin Sep 12 '17 at 13:29
-1

You must use a single quote. When you use double quote, the special characters are not taken literally so whenever you use a backslash you must skip it with another backslash. When you use a single quote things are taken literally so you don't to skip special characters "spaces, dollar signs...etc.".

You may google to read more about differences between single and double quotes in a Linux Bourne-Again shell.

  • is it not a must, I want to pass variables to sed, so I need double quotes. – Mohammed Noureldin Oct 3 '17 at 9:41
  • Sorry if I miss something. I don't think it matters if you use single or double quote with sed as long as you take your precautions of using escape characters appropriately. I read in some other thread (and it is obvious in your case) that both '\' and 'n' need to be escaped for your command to work as you expect. You may check here: stackoverflow.com/questions/723157/… – Amjad Abdullah Oct 4 '17 at 14:11
  • Actually it does matter, you can read this here superuser.com/questions/1249828/… but the behaviour of three backslashes is still weird for me. – Mohammed Noureldin Oct 4 '17 at 15:00
  • Thanks for pointing me to the useful thread. It seconds what I was saying as it shouldn't matter about using the quotes as long as you take your precautions of using escape characters appropriately The link of your question answers your concern exactly. I don't know why you are still having doubts. My doubt though is why there are 2 different translations by bash itself and by echo while echo is a shell built-in? I was assuming that the bash translation should exactly be treated by echo. But it seems my assumption is not valid especially it contradicts with the practical behavior. – Amjad Abdullah Oct 8 '17 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.