How do I get $(/bin/printf -6) to return -6 and not think -6 is an option

允我心安 提交于 2019-12-01 19:43:27

What if you called printf with an actual format string?

$ printf "%d\n" -6
-6
$ /sbin/busybox printf "%d\n" -6
-6
$

This works with both GNU coreutils' and busybox' printf, apparently.

Most GNU programs support using -- as a delimiter to tell the program that all further arguments are not options. For instance

$ printf -- -6
-6

You should use

printf -- -6

If you feed a non-numeric argument in this way, you get an error message:

$ busybox printf "%d" "a"
a: conversion error
-1

But you can use %s and it will work for both numeric and non-numeric arguments (as long as you don't need to do any formatting):

$ busybox printf "%s" "a"
a
$ busybox printf "%s" -6
-6

If you're not using the formatting features of printf and you need to output the value without a newline, busybox's echo command supports -n:

$ busybox echo -n "a"
a
$ busybox echo -n -6
-6
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!