简体   繁体   English

bash:printf(“%.s”)在s之后不保留空格

[英]bash: printf (“%.s ”) doesn't keep whitespaces after the s

I have a piece of code like this: 我有一段这样的代码:

printf "%.s  "  $(seq 1 $count)

It actually belongs to an else condition and its job is to print out whitespaces $count times.. 它实际上属于else条件,其工作是打印$ count次的空格。

It works fine if I enter a string like this: 如果我输入像这样的字符串,它可以正常工作:

printf "%.shelloworld  "  $(seq 1 $count)

but not when i just put in whitespaces. 但是当我只在空白处输入时不可以。

Any work arounds? 有什么解决方法吗?

Works for me: 为我工作:

# printf "%.s  "  $(seq 1 10) | hexdump -C
00000000  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000010  20 20 20 20                                       |    |

Is this the exact command that's being entered, or is there other variable substitution happening? 这是正在输入的确切命令,还是正在发生其他变量替换? Because the exact thing you seem to be experiencing would happen if you didn't quote a particular variable expansion: 因为如果您不引用特定的变量扩展,似乎就会发生确切的事情:

# frm="%.s  "; printf $frm $(seq 1 10) | hexdump -C
[no output]

Whereas: 鉴于:

# frm="%.s  "; printf "$frm" $(seq 1 10) | hexdump -C
00000000  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
00000010  20 20 20 20                                       |    |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM