linux shell寫入單行、多行內容到文件


1、單行文本

#! /bin/bash
echo 'hello world' > filename.txt

2、多行文本

代碼1:


#! /bin/bash
cat>filename.txt<<EOF
hello world
代碼改變世界 Coding Changes the World
100 \$
她買了張彩票,中了3,300多萬美元。
She bought a lottery ticket and won more than\$ 33 million.
EOF

 

代碼2:

 

#! /bin/bash
filename="/test/filename.txt"
cat>"${filename}"<<EOF
hello world
代碼改變世界 Coding Changes the World
100 \$
她買了張彩票,中了3,300多萬美元。
She bought a lottery ticket and won more than\$ 33 million.
EOF

 

說明:
1.其中,<<EOF 表示當遇到EOF時結束輸入。
2.cat>test1.txt<<EOF 這間沒有空格
3.$號要加\轉義字符


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM