linux - Is there any difference between the three assignment ways: a=1, let a=1, ((a=1)) -
consider 3 ways assign value:
#!/bin/bash a=1 let a=1 ((a=1))
are 3 assignments totally equal?
if shell has non-posix extensions (ie. ksh or bash), they're entirely equivalent: a=1
string assignment, whereas let a=1
, (( a=1 ))
numeric assignments, gets stored in a
string representing number 1
in every case.
$(( ))
posix-compliant way create math context. (( ))
, let
, contrast, not specified posix, extensions available in shells go beyond standard.
now, consider instead:
b=2 # result | posix? | type | comments # --------+---------+--------------------+---------------- a=b # a=b | true | string assignment | let a=b # a=2 | false | numeric assignment | ancient non-posix syntax a=$((b)) # a=2 | true | numeric assignment | modern posix syntax ((a=b)) # a=2 | false | numeric assignment | modern non-posix extension
Comments
Post a Comment