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.$號要加\轉義字符