回声$(printf的...)下降空白/格式化 [英] echo $(printf ...) dropping whitespace/formatting

查看:86
本文介绍了回声$(printf的...)下降空白/格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用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屋!

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