You're not quoting the variable OUT_TXT
when you use it with echo
. This causes the shell to split the variable's value on spaces, tabs and newlines into words that are then given as arguments to echo
(after additionally undergoing filename globbing expansion). echo
then outputs those arguments with spaces in-between them.
Quote you variable expansions, always:
echo "$OUT_TXT"
Also note that you have an issue in echo $IN_TXT
. If the expansion of the IN_TXT
variable isn't quoted as "$IN_TXT"
, its value would be used as a globbing pattern and would be replaced by any matching filename (test this with IN_TXT='*'
, for example).
A similar issue is present in your sed
expression, since it's not properly quoted.
See also:
Also, your sed
command is better wirtten as sed 'y/*/\n/'
as standard sed
simply can't insert newlines with the s///
command (GNU sed
is an exception). The sed
command y///
does know how to change single characters into newlines, regardless of what implementation of sed
is used.
Or, you could simply use the tr
command as tr '*' '\n'
, or a quick variable substitution in the shell:
printf '%s\n' "${IN_TXT//\*/ }"
or,
OUT_TXT=${IN_TXT//\*/ }
printf '%s\n' "$OUT_TXT"
sed 's/*/\n/g'
instead ofsed s/*/\\n/g
echo "$IN_TXT" | sed 's/*/\n/g'