51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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.$号要加\转义字符
如果觉得这文章还算用心,请劳驾点击右下角的推荐,这是对我们这些做开源分享的最大的肯定,谢谢。
作者:zqifa

出处:http://www.cnblogs.com/zqifa/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。


赞(1)
未经允许不得转载:工具盒子 » linux shell写入单行、多行内容到文件