如何在没有换行符的情况下回声? [英] How to echo out things without a newline?
本文介绍了如何在没有换行符的情况下回声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码...
for x in ${array[@]}
do
echo $x
done| sort
结果是这样的
1
2
3
4
5
是否可以将其打印为1 2 3 4 5
?不必每次都添加换行符吗?
Is there a way to print it as 1 2 3 4 5
instead? Without adding a newline every time?
我也尝试过另一篇文章建议的printf
,但它仍在打印新行
I also tried printf
as suggested by another post, but it's still printing new lines
for x in ${array[@]}
do
printf '%s' "$x"
done| sort
推荐答案
是.使用-n
选项:
echo -n $x
来自help echo
:
-n do not append a newline
这也会剥离最后一个换行符,因此,如果需要,您可以在循环后添加最后一个换行符:
This would strips off the last newline too, so if you want you can add a final newline after the loop:
for ...; do ...; done; echo
注意:
在内置/外部可执行文件echo
的各种实现中,这是不可移植的.可移植的方法是使用printf
代替:
This is not portable among various implementations of echo
builtin/external executable. The portable way would be to use printf
instead:
printf '%s' "$x"
这篇关于如何在没有换行符的情况下回声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文