質問

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
役に立ちましたか?

解決

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
$ 

他のヒント

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)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top