Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: unescape string

Tags:

bash

Suppose I have the following string:

"some\nstring\n..."

And it displays as one line when catted in bash. Further,

string_from_pipe | sed 's/\\\\/\\/g' # does not work
| awk '{print $0}'
| awk '{s = $0; print s}'
| awk '{s = $0; printf "%s",s}'
| echo $0  
| sed 's/\\(.)/\1/g'
# all have not worked. 

How do I unescape this string such that it prints as:

some
string

Or even displays that way inside a file?

like image 392
Chris Avatar asked Nov 29 '17 23:11

Chris


3 Answers

POSIX sh provides printf %b for just this purpose:

s='some\nstring\n...'
printf '%b\n' "$s"

...will emit:

some
string
...

More to the point, the APPLICATION USAGE section of the POSIX spec for echo explicitly suggests using printf %b for this purpose rather than relying on optional XSI extensions.

like image 84
Charles Duffy Avatar answered Nov 13 '22 10:11

Charles Duffy


As you observed, echo does not solve the problem:

$ s="some\nstring\n..."
$ echo "$s"
some\nstring\n...

You haven't mentioned where you got that string or which escapes are in it.

Using a POSIX-compliant shell's printf

If the escapes are ones supported by printf, then try:

$ printf '%b\n' "$s"
some
string
...

Using sed

$ echo "$s" | sed 's/\\n/\n/g'
some
string
...

Using awk

$ echo "$s" | awk '{gsub(/\\n/, "\n")} 1'
some
string
...
like image 5
John1024 Avatar answered Nov 13 '22 10:11

John1024


If you have the string in a variable (say myvar), you can use:

${myvar//\\n/$'\n'}

For example:

$ myvar='hello\nworld\nfoo'
$ echo "${myvar//\\n/$'\n'}"
hello
world
foo
$ 

(Note: it's usually safer to use printf %s <string> than echo <string>, if you don't have full control over the contents of <string>.)

like image 1
psmears Avatar answered Nov 13 '22 10:11

psmears