回声附加文字 [英] Echo Appending Text

查看:52
本文介绍了回声附加文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在终端机中,并且在文件中写入了一些基本字符串,是否可以使用echo连续地追加到该文件?

If I'm in terminal and I write some basic string to a file, is there a way to continually append to that file using echo?

例如,echo'hello'>文件会将'hello'放入该文件,但是如果我现在想在文件后附加'world'怎么办?我知道,如果我回显世界,它将覆盖我写入文件的第一个字符串。我可以在bash中使用+ =运算符吗?

For instance, echo 'hello' > file will put 'hello' into that file, but what if I now want to append ' world' to file? I know that if I do echo ' world', it'll overwrite the first string I wrote into file. Is there any += operator I can use in bash?

编辑:没关系,我可以使用>>追加。可以附加到同一行而不是新行吗?

Nevermind, I can append using >>. Is it possible to append to the same line instead of to a new line?

推荐答案

echo -n 'Hello' > file
echo ', World!' >> file

>> 重定向运算符

这里我们使用的是 echo 的非标准扩展名,其中 -n 告诉它不要添加新行。

Here we're using a non-standard extension of echo, where -n tells it not to append a new line.

如果您想要符合标准的行为,请使用 printf

If you want standard-compliant behavior, use printf:

printf 'Hello' > file
printf ', World!\n' >> file

编辑:在包含感叹号的字符串周围使用双引号!可能不适用于某些版本。它可以被解释为历史扩展令牌。使用单引号防止出现这种情况。

using double quote around a string containing the exclamation ! may not work with some versions. It could get interpreted as a history expansion token. Use single quotes to prevent this.

这篇关于回声附加文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆