How to replace "\n" string with a new line in Unix Bash script
No need for sed
, using parameter expansion:
$ foo='1\n2\n3'; echo "${foo//'\n'/$'\n'}" 123
With bash 4.4
or newer, you can use the E
operator in ${parameter@operator}
:
$ foo='1\n2\n3'; echo "${foo@E}"123
Other answers contain alternative solutions. (I especially like the parameter expansion one.)
Here's what's wrong with your attempt:
In
echo $EXT_DESCR | sed 's/\\n/\n/g'
the sed command is in single quotes, so sed gets s/\\n/\n/g
as is.
In
NEW_DESCR=`echo $EXT_DESCR | sed 's/\\n/\n/g'`
the whole command is in backticks, so a round of backslash processing is applied. That leads to sed getting the code s/\n/\n/g
, which does nothing.
A possible fix for this code:
NEW_DESCR=`echo $EXT_DESCR | sed 's/\\\\n/\\n/g'`
By doubling up the backslashes, we end up with the right command in sed.
Or (easier):
NEW_DESCR=$(echo $EXT_DESCR | sed 's/\\n/\n/g')
Instead of backticks use $( )
, which has less esoteric escaping rules.
Note: Don't use ALL_UPPERCASE
for your shell variables. UPPERCASE
is (informally) reserved for system variables such as HOME
and special built-in variables such as IFS
or RANDOM
.
This printf
would do the job by interpreting all escaped constructs:
printf -v NEW_DESCR "%b" "$EXT_DESCR"
-v
option will store output in a variable so no need to use command substitution here.
Problem with your approach is use of old back-ticks. You could do:
NEW_DESCR=$(echo "$EXT_DESCR" | sed 's/\\n/\n/g')
Assuming you're using gnu sed
as BSD sed
won't work with this approach.