Bash:为什么 echo 增加了额外的空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2003536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: Why is echo adding extra space?
提问by Dan
I get:
我得到:
$ echo -e "D"{a,b,c}".jpg\n"
Da.jpg
Db.jpg
Dc.jpg
Note: The extra spaces before Db and Dc on the 2nd and 3rd line of the output.
注意:输出第 2 行和第 3 行 Db 和 Dc 之前的额外空格。
Why are these there?
为什么会有这些?
Thanks, Dan
谢谢,丹
Edit: Since my actual objective had spaces in it (which I should have written originally):
编辑:因为我的实际目标中有空格(我本来应该写的):
echo -e "Name"{,.}" "{-,}"extra"{,so}" 5v5 "{one,two,No\ four}{,!,\!\!}"\n"
Most solutions here didn't work for me (for loop, xarg, tr). Printf didn't work because of multiple braces expansions that I want to cantesian product.
这里的大多数解决方案对我都不起作用(for 循环、xarg、tr)。由于我想要 Cantesian 产品的多个大括号扩展,Printf 不起作用。
I combined 3 solutions (mletterle's \b, Dennis Williamson's extra space, and Jim Dennis's using far less quotes) to get:
我结合了 3 个解决方案(mletterle 的 \b、Dennis Williamson 的额外空间和 Jim Dennis 使用更少的引号)得到:
echo -e "" \bName{,.}\ {-,}extra{,so}\ 5v5\ {one,two,No\ four}{,\!,\!\!}\n
Thanks all who answered! I learned a lot from your responses!
感谢所有回答的人!我从你的回答中学到了很多!
Dan
担
采纳答案by mletterle
The easiest and cleanest solution is to add a backspace to the front of each line:
最简单和最干净的解决方案是在每一行的前面添加一个退格:
echo -e -n "\bD"{a,b,c}".jpg\n"
This produces the desired output.
这会产生所需的输出。
回答by James Polley
Because that's what brace expansion does. From man bash, under the heading Brace expansion:
因为这就是大括号扩展所做的。从man bash,在标题下Brace expansion:
Patterns to be brace expanded take the form of an optional preamble, followed by ... a series of comma-separated strings ... followed by an optional postscript. The preamble is prefixed to each string contained within the braces, and the postscript is then appended to each resulting string, expanding left to right For example, a{d,c,b}e expands into ‘ade ace abe'
大括号扩展的模式采用可选的前导码形式,后跟 ... 一系列逗号分隔的字符串 ... 后跟可选的后记。大括号内包含的每个字符串都以前导码为前缀,然后将后记附加到每个结果字符串,从左到右扩展 例如,a{d,c,b}e 扩展为 'ade ace abe'
So in your example, "D" is the preamble and ".jpg\n" is the postscript.
所以在你的例子中,“D”是序言,“.jpg\n”是后记。
So, after brace expansion occurs, you're left with:
因此,在发生大括号扩展后,您将剩下:
echo -e Da.jpg\n Db.jpg\n Dc.jpg\n
echo -e Da.jpg\n Db.jpg\n Dc.jpg\n
As hewgill points out, the shell then splits this into three tokens and passes them to echo; which outputs each token separated by a space. To get the output you want, you need to use one of the many suggestions here that don't re-inserted the unwanted space between tokens.
正如 hewgill指出的那样,shell 然后将其拆分为三个标记并将它们传递给 echo;它输出由空格分隔的每个标记。要获得所需的输出,您需要使用此处的众多建议之一,这些建议不会在令牌之间重新插入不需要的空间。
It's longer and probably not the neatest way to do this, but the following gives the output you're after:
它更长,可能不是最简洁的方法,但以下给出了您所追求的输出:
for file in "D"{a,b,c}".jpg"
do
echo ${file}
done
回答by ghostdog74
use the more portable printf
使用更便携的 printf
$ printf "D%s.jpg\n" {a,b,c}
Da.jpg
Db.jpg
Dc.jpg
回答by Greg Hewgill
echoalways adds spaces between arguments. Try your command without \nand compare the results.
echo总是在参数之间添加空格。试试你的命令\n并比较结果。
回答by alvherre
You can get the desired effect by using xargs to separate the arguments spit by the first echo into a line each:
您可以通过使用 xargs 将第一个 echo 吐出的参数分别分成一行来获得所需的效果:
$ echo "D"{a,b,c}".jpg" | xargs -n1 echo
Da.jpg
Db.jpg
Dc.jpg
回答by Paused until further notice.
You can get a more consistent look by prepending a null:
您可以通过添加 null 来获得更一致的外观:
$ echo -en "" "D"{a..c}".jpg\n"
Da.jpg
Db.jpg
Dc.jpg
Now they all have an extra space. Also, using -neliminates the extra newline at the end. Also, you can use a range in your brace expansion.
现在他们都有一个额外的空间。此外, using-n消除了末尾的额外换行符。此外,您可以在大括号扩展中使用范围。
回答by daneel
Here is a solution using sed(that builds upon https://stackoverflow.com/a/2003856/8180143):
这是使用的解决方案sed(基于https://stackoverflow.com/a/2003856/8180143):
$ echo -en "" "D"{a..c}".jpg\n" | sed 's/ //'
Da.jpg
Db.jpg
Dc.jpg
This solution has the advantage of working with inputs having spaces, e.g.,
该解决方案的优点是可以处理具有空格的输入,例如,
$ echo -en "" "D "{a..c}".jpg\n" | sed 's/ //'
D a.jpg
D b.jpg
D c.jpg

