shell - Variable assignment in Unix -
this question has answer here:
when try following statements, doesn't assign values variables , loop goes infinite-
while ( [ $nos -ne 0 ] ) rem = `expr $nos % 10` sum = `expr $sum + $rem` nos = `expr $nos / 10` done but when remove spaces left , right side of assignment operator, works fine. -
while ( [ $nos -ne 0 ] ) rem=`expr $nos % 10` sum=`expr $sum + $rem` nos=`expr $nos / 10` done why shell behaviour that?
rem = `expr $nos % 10` runs command rem first argument =.
if weren't posix sh standard requiring assignments parse single word (which does), 1 couldn't pass = first argument command or function; hence, standard being written is.
there's further expressive power based on whitespace in assignments well. instance:
rem= foo ...runs foo environment variable rem set empty string duration of single command's execution, as
var=val foo sets var val while running foo. useful syntax; if var=val foo equivalent var="val foo", not possible.
Comments
Post a Comment