I am making bash script and I want to replace one character with another character in my string variable.
Example:
#!/bin/sh
string="a,b,c,d,e"
And I want to replace ,
with \n
.
output:
string="a\nb\nc\nd\ne\n"
How can I do it?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communitySo many ways, here are a few:
$ string="a,b,c,d,e"
$ echo "${string//,/$'\n'}" ## Shell parameter expansion
a
b
c
d
e
$ tr ',' '\n' <<<"$string" ## With "tr"
a
b
c
d
e
$ sed 's/,/\n/g' <<<"$string" ## With "sed"
a
b
c
d
e
$ xargs -d, -n1 <<<"$string" ## With "xargs"
a
b
c
d
e
,
with literal\n
or newline?