シェルで改行を表現する方法は?

シェルで改行を表現する方法は?

私はdebian7.8 bashシェルを使用しています。

str="deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free  \n  
      deb-src http://ftp.cn.debian.org/debian/ wheezy main contrib non-free  "

echo $str > test  

私のテストファイルでは、次のようになります。

deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free \n deb-src http://ftp.cn.debian.org/debian/ wheezy main contrib non-free

私が望むもの:

deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free 
deb-src http://ftp.cn.debian.org/debian/ wheezy main contrib non-free

改行を正しく表現する方法は?

ベストアンサー1

Jasonwryanの提案に加えて、以下を使用することをお勧めしますprintf

$ printf "%s http://ftp.cn.debian.org/debian/ wheezy main contrib non-free\n" deb deb-src > test
$ cat test
deb http://ftp.cn.debian.org/debian/ wheezy main contrib non-free
deb-src http://ftp.cn.debian.org/debian/ wheezy main contrib non-free

printfフォーマット文字列は引数が使い果たされるまで再利用されるため、繰り返し行を印刷するための良い方法が提供されます。

おすすめ記事