yy 发帖数: 45 | 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 | w*****e 发帖数: 23 | 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
| yy 发帖数: 45 | 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"
|
|