-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?

2 Answers2

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/
-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.