Replace character X with character Y in a string with bash
86,845
So 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
Related videos on Youtube
Author by
Joozty
Updated on September 18, 2022Comments
-
Joozty over 1 year
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?
-
Admin about 8 yearsYou want to replace
,
with literal\n
or newline?
-