【发布时间】:2015-12-24 01:51:31
【问题描述】:
我写了以下代码:
case "$2" in '+') ((res = $1 + $2 ));;
'-') ((res = $1 - $2 ));;
esac
echo $res
使用示例:./"*filename*" 2 + 3
如果我使用双括号,
((res = $1 + $2 ))
然后打印结果。但是,如果我使用单括号
(res = $1 + $2 )
然后什么都不会打印。 ( )和(( ))有什么区别?
【问题讨论】:
-
你使用的是哪个外壳?
-
((...))用于arithmetic expansion,而(...)在子shell 中运行封闭的命令,这不会影响其父shell。 -
另外,请注意,在您的示例中,第二个参数 (
$2) 是运算符,而不是第二个操作数。因此,您可能需要((res = $1 + $3))和((res = $1 - $3)),在这里。 -
@Jubobs 这些 cmets 可以作为答案发布。
-
看看shell手册怎么样?它解释了
( ... )和(( ... ))。在 Linux 上可能是man bash。
标签: shell unix switch-statement arithmetic-expressions subshell