I have a shell script that is called via parameters (it's called by an external binary programm which I can not change), like this:
myscript.sh "param1" "param2"
Now, in this script there's a sed "s/param1/param2/"
-like command involved and the param2
can contain literaly the newline escape sequence \n
(like line1\nline2
):
VAL=$(echo "$2" | sed -e 's/[\/&]/\\&/g')
sed -i "s/$1/$VAL/" /a/path/to/file
I already did this: Escape a string for a sed replace pattern to escape backslashes and ampersands that may occur, but this does not help handling the newline \n
(sed
ignores it).
I know how to do it manually in a script (by entering a real newline, pressing Return, in the shell script file at the according place or do some stuff like $(echo)
), but I have no influence to the parameters that are passed.
How can I safely handle the newline sequence so that sed does its job and inserts a newline when \n
occurs in the parameter?