如何将字符串追加到同一行而不是创建新行? [英] How to append strings to the same line instead of creating a new line?

查看:47
本文介绍了如何将字符串追加到同一行而不是创建新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重定向到文件对于将字符串作为新行附加到文件中非常有用,例如

Redirection to a file is very usefull to append a string as a new line to a file, like

echo "foo" >> file.txt
echo "bar" >> file.txt

结果:

foo
bar

但是也可以将字符串重定向到文件中的同一行吗?

But is it also possible to redirect a string to the same line in the file ?

示例:

echo "foo" <redirection-command-for-same-line> file.txt
echo "bar" <redirection-command-for-same-line> file.txt

结果:

foobar

推荐答案

换行符是通过 echo 添加的,而不是通过重定向添加的.只需将 -n 开关传递到 echo 即可将其隐藏:

The newline is added by echo, not by the redirection. Just pass the -n switch to echo to suppress it:

echo -n "foo" >> file.txt
echo -n "bar" >> file.txt

-n 不输出尾随换行符

这篇关于如何将字符串追加到同一行而不是创建新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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