avatar
yy
1
ne short script:
ns=0
if [ $# -eq 3 ]; then
ans=$(( $n1 $op $n2 ))
echo "$n1 $op $n2 = $ans"
return $ans
else
echo "Function cal requires atleast three args"
fi
return
}
cal 5 + 10
cal 10 - 2
cal 10 / 2
echo $?
Run result:
$ ./pass1
5 + 10 = 15
10 - 2 = 8
10 / 2 = 5
5
My question is:
why it is: ans=$(( $n1 $op $n2 ))
not: ans=( $n1 $op $n2 ) ***
I tried the *** one: the running result is:
5 + 10 = 5
10 - 2 = 10
10 / 2 = 10
10
can any exlain a little bit?
Thank you very much
s
avatar
w*e
2
(( ... )) is special characters in Bash, means expand and evaluate integer
expression between (( )).
( one two three ) is just array, if you use A=(B C), just means A is assigned
as array, so when you echo, it just dump first element which is B out.
You may want to look at "Advanced Bash scripting"

【在 yy 的大作中提到】
: ne short script:
: ns=0
: if [ $# -eq 3 ]; then
: ans=$(( $n1 $op $n2 ))
: echo "$n1 $op $n2 = $ans"
: return $ans
: else
: echo "Function cal requires atleast three args"
: fi
: return

avatar
yy
3
Thanks a lot!

【在 w*****e 的大作中提到】
: (( ... )) is special characters in Bash, means expand and evaluate integer
: expression between (( )).
: ( one two three ) is just array, if you use A=(B C), just means A is assigned
: as array, so when you echo, it just dump first element which is B out.
: You may want to look at "Advanced Bash scripting"

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。