追加的方式有两种<<和tee,后者可以同时给多个文件追加,给一个文件追加一般用前一种
参考资料:https://linuxize.com/post/bash-append-to-file/

1. >>

">>"是redirect指令,常于echo,printf和cat指令一起用,操作方法:

1.2 echo + <<

echo "hello world " >> a.txt

效果:
在这里插入图片描述
echo用上-e参数可以在里面使用反转义字符

echo -e "hello  a new world \n" >> a.txt

这里我们在"hello a new world \n"后面加个换行,效果如下
在这里插入图片描述

1.3 printf+ >>

printf比echo支持更复杂的操作,比如可以设定输出的格式
在这里插入图片描述
这里翻车了,user找不到

1.4 heredoc

使用cat<>filename,可以使用heredoc,直接编辑即可加入

在这里插入图片描述
这里需要注意>>符号于>符号有所不同,后者会直接覆盖
在这里插入图片描述

2.tee

2.1实现添加到单个文件

echo "hello world" |tee -a a.txt

-a表示append,也可以再加一个选项,不显示在控制台上
在这里插入图片描述
不加a会直接覆盖:
在这里插入图片描述

2.2 追加到多个文件

在这里插入图片描述

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐