Which is the preferred way of doing integer math in Bash?
A. a=$(expr 17 + 23)
B. b=$[17 + 23]
C. ((c=17+23))
D. let d="17 + 23"
Answer: C
Using $( expr ) spawns a subshell and runs the expr command in it. As such it requires more resources and undergoes globbing, pathname expansion and word splitting like any other command. Using (( )) is preferred.
The $[ ] form is equivalent to (( )) but according to the bash manpage it has been deprecated and may be removed in the future. Using (( )) is preferred.
Let is a builtin command in bash and as such undergoes globbing, pathname expansion and word splitting like any other command. Using (( )) is preferred.
No comments:
Post a Comment