Pregunta

I only see spaces, no new lines.

script.sh:

STRING=$(echo "alpha,beta,gamma,delta" | tr "," "\n")
echo $STRING > string.txt

Result:

$ cat string.txt
alpha beta gamma delta

Desired result

$ cat string.txt
alpha
beta
gamma
delta
¿Fue útil?

Solución

Try quoting the $STRING variable:

echo "$STRING" > string.txt

e.g:

$ STRING=$(echo "alpha,beta,gamma,delta" | tr "," "\n")
$ echo $STRING
alpha beta gamma delta
$ echo "$STRING"
alpha
beta
gamma
delta
$ 

Otros consejos

BASH / shell doesn't usually work right without quoting, make sure to use it here to get new lines in your file:

echo "$STRING" > string.txt

See official manual on world splitting (Thanks to Glenn)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top