【发布时间】:2018-10-29 06:55:21
【问题描述】:
我想让脚本 /bin/sh 兼容。在某些时候,我正在使用将十六进制变量打印到十进制值,但它抛出了这个错误:
sh: 1: arithmetic expression: expecting EOF: "16#c0"
当脚本由 /bin/bash 执行时,该错误不存在。我把它归结为以下问题:
$ sh -c 'echo $((16#c0))'
sh: 1: arithmetic expression: expecting EOF: "16#c0"
$ sh -c "echo $((16#c0))"
192
为什么会这样?如何让 echo 在我的脚本中工作?
编辑: subshell 被重定向到 /bin/dash
$ readlink -f $(which sh)
/bin/dash
$ type sh
sh is /bin/sh
【问题讨论】:
-
你肯定需要
bash -c '...'让bash执行它吗? -
@MarkSetchell:我假设 OP 希望在符合 POSIX 的 shell 中执行此操作
-
没错,我想让它由 sh 执行