我如何获得$(/bin/printf -6)返回-6却不认为-6是一个选择 [英] How do I get $(/bin/printf -6) to return -6 and not think -6 is an option
问题描述
我有一个bash shell脚本,其内容如下:
I have a bash shell script which has the line:
g=$(/bin/printf ${i})
当${i}
包含类似-6
的内容时,printf
认为它被传递了一个选项.它无法识别该选项,因此会产生错误.
when ${i}
contains something like -6
, printf
thinks its being passed an option. It does not recognize the option so produces an error.
如果将${i}
用引号引起来,则printf
仍认为它被传递了一个选项.
if wrap ${i}
in quotes, printf
still thinks its being passed an option.
g=$(/bin/printf "${i}")
如果我不使用引号,则变量$g
会保留"-6
",这也不是我想要的.
if I escape the quotes, variable $g
then holds "-6
" which is not what I want either.
g=$(/bin/printf \"${i}\")
那里是逃脱破折号(-)的地方.
Is there away to escape the dash (-).
printf是BusyBox应用
printf is a BusyBox app
推荐答案
如果您使用实际的格式字符串调用printf
怎么办?
What if you called printf
with an actual format string?
$ printf "%d\n" -6
-6
$ /sbin/busybox printf "%d\n" -6
-6
$
这显然适用于GNU coreutils和busybox的printf.
This works with both GNU coreutils' and busybox' printf, apparently.
这篇关于我如何获得$(/bin/printf -6)返回-6却不认为-6是一个选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!