回声$(printf的...)下降空白/格式化 [英] echo $(printf ...) dropping whitespace/formatting
问题描述
我用printf格式化在bash一个数字:
I was using printf to format a number in bash:
$ printf -- ">>%4d\n" 1
>> 1
这工作得很好,但是当我做同样的事情在子shell:
This works fine, but when i do the same thing in a subshell:
$ echo $(printf -- ">>%4d\n" 1)
>> 1
为什么空格去掉?我完全不知道。
Why are the spaces removed? I have absolutely no idea.
推荐答案
子shell是不是这样做。不能直接使用。
The sub-shell isn't doing it. Not directly.
这里的问题是,你是不是引用子shell的结果。这样的壳字拆分得到的文本(这滴多余的空格),然后双手单词列表来呼应其高兴地吐回了你(没有额外的空格)。
The issue here is that you aren't quoting the sub-shell result. As such the shell is word-splitting the resulting text (which drops extraneous spaces) and then hands a list of words to echo which happily spits them back out at you (without the extra spaces).
这是本质上没有比运行不同的回声1
键,想知道多余的空格去了。
This is essentially no different than running echo 1
and wondering where the extra spaces went.
我的回答这里此讨论了一下为好。
My answer here discusses this a bit as well.
这篇关于回声$(printf的...)下降空白/格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!