How to replace “/n” string with a new line in Unix Bash script

  • A+
Category:Languages

Cannot seem to find an answer to this one online...

I have a string variable (externally sourced) with new lines "/n" encoded as strings.

I want to replace those strings with actual new line carriage returns. The code below can achieve this...

echo $EXT_DESCR | sed 's///n//n/g' 

But when I try to store the result of this into it's own variable, it converts them back to strings

NEW_DESCR=`echo $EXT_DESCR | sed 's///n//n/g'` 

How can this be achieved, or what I'm I doing wrong?

Here's my code I've been testing to try get the right results

EXT_DESCR="This is a text/nWith a new line" echo $EXT_DESCR | sed 's///n//n/g'  NEW_DESCR=`echo $EXT_DESCR | sed 's///n//n/g'` echo "" echo "$NEW_DESCR" 

 


No need for sed, using parameter expansion:

$ foo='1/n2/n3'; echo "${foo//'/n'/$'/n'}"   1 2 3 

With bash 4.4 or newer, you can use the E operator in ${parameter@operator}:

$ foo='1/n2/n3'; echo "${foo@E}" 1 2 3 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: