Skip to main content
added 210 characters in body; added 3 characters in body
Source Link
user16402
user16402

Bash

  

Since this is a , I guess I should use a long-winded method...

v=2 #v is 2 v+=2 #v is 4 v=$(($v*5)) #v is 20 v=$(($v-16)) #v is 4 v=`bc<<<"sqrt($v)+2"` #v is 4 (sqrt(4) is 2) v=`bc<<<"$v/4+3"` #v is 4 (4/4 = 1) echo '2+2=' $v #So v is 4...? 

For people who don't know Bash: $((...expr...)) is a syntax to evaluate arithmetic expressions. $(bc<<<...expr...) does the same using the bc command-line calculator.

v=2 #v is 2 v+=2 #v is 4 v=$(($v*5)) #v is 20 v=$(($v-16)) #v is 4 v=$(bc<<<"sqrt($v)+2") #v is 4 (sqrt(4) is 2) v=$(bc<<<"$v/4+3") #v is 4 (4/4 = 1) echo '2+2=' $v #So v is 4...? 

Output

2+2= 5 
2+2= 5 

Explanation

The second line concatenates v and 2 instead of adding them, to make 22.
Actual explanation:
! v=2 #v is 2 ! v+=2 #v is 22 ! v=$(($v*5)) #v is 110 ! v=$(($v-16)) #v is 94 ! v=$(bc<<<"sqrt($v)+2") #v is 11 (by default, bc rounds to integers) ! v=$(bc<<<"$v/4+3") #v is 5 (11/4 is 2 with rounding) ! echo '2+2=' $v #TADAAAM

Bash

 

Since this is a , I guess I should use a long-winded method...

v=2 #v is 2 v+=2 #v is 4 v=$(($v*5)) #v is 20 v=$(($v-16)) #v is 4 v=`bc<<<"sqrt($v)+2"` #v is 4 (sqrt(4) is 2) v=`bc<<<"$v/4+3"` #v is 4 (4/4 = 1) echo '2+2=' $v #So v is 4...? 

Output

2+2= 5 

Explanation

The second line concatenates v and 2 instead of adding them, to make 22.
Actual explanation:
! v=2 #v is 2 ! v+=2 #v is 22 ! v=$(($v*5)) #v is 110 ! v=$(($v-16)) #v is 94 ! v=$(bc<<<"sqrt($v)+2") #v is 11 (by default, bc rounds to integers) ! v=$(bc<<<"$v/4+3") #v is 5 (11/4 is 2 with rounding) ! echo '2+2=' $v #TADAAAM

Bash

 

Since this is a , I guess I should use a long-winded method...

For people who don't know Bash: $((...expr...)) is a syntax to evaluate arithmetic expressions. $(bc<<<...expr...) does the same using the bc command-line calculator.

v=2 #v is 2 v+=2 #v is 4 v=$(($v*5)) #v is 20 v=$(($v-16)) #v is 4 v=$(bc<<<"sqrt($v)+2") #v is 4 (sqrt(4) is 2) v=$(bc<<<"$v/4+3") #v is 4 (4/4 = 1) echo '2+2=' $v #So v is 4...? 

Output

2+2= 5 

Explanation

The second line concatenates v and 2 instead of adding them, to make 22.
Actual explanation:
! v=2 #v is 2 ! v+=2 #v is 22 ! v=$(($v*5)) #v is 110 ! v=$(($v-16)) #v is 94 ! v=$(bc<<<"sqrt($v)+2") #v is 11 (by default, bc rounds to integers) ! v=$(bc<<<"$v/4+3") #v is 5 (11/4 is 2 with rounding) ! echo '2+2=' $v #TADAAAM

Source Link
user16402
user16402

Bash

Since this is a , I guess I should use a long-winded method...

v=2 #v is 2 v+=2 #v is 4 v=$(($v*5)) #v is 20 v=$(($v-16)) #v is 4 v=`bc<<<"sqrt($v)+2"` #v is 4 (sqrt(4) is 2) v=`bc<<<"$v/4+3"` #v is 4 (4/4 = 1) echo '2+2=' $v #So v is 4...? 

Output

2+2= 5 

Explanation

The second line concatenates v and 2 instead of adding them, to make 22.
Actual explanation:
! v=2 #v is 2 ! v+=2 #v is 22 ! v=$(($v*5)) #v is 110 ! v=$(($v-16)) #v is 94 ! v=$(bc<<<"sqrt($v)+2") #v is 11 (by default, bc rounds to integers) ! v=$(bc<<<"$v/4+3") #v is 5 (11/4 is 2 with rounding) ! echo '2+2=' $v #TADAAAM