Bash Shell built-in Let command
In Linux let command used for Evaluate arithmetic expressions. Shell variables are allowed as operands. The variable need not have its integer attribute turned on to be used in an expression.
We can easily found handy help with “help let” command.
The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.
id++, id-- variable post-increment, post-decrement ++id, --id variable pre-increment, pre-decrement -, + unary minus, plus !, ~ logical and bitwise negation ** exponentiation *, /, % multiplication, division, remainder +, - addition, subtraction <<, >> left and right bitwise shifts <=, >=, <, > comparison ==, != equality, inequality & bitwise AND ^ bitwise XOR | bitwise OR && logical AND || logical OR expr ? expr : expr conditional operator =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= assignment
let is very similar to ((. But let is simple way to do arithmetic operation.let see some examples how we use this.
#a=12;b=24; let c=a+b; echo $c 36 #a=12; let a++; echo $a 13 #a=12; let a--; echo $a 11 #a=12; let b=a/2;echo $b 6 #let a=2**12 ; echo $a 4096 #a=12;b=24; c=a+b; echo $c a+b #a=12;b=24; c=$((a+b)); echo $c 36
In above examples, you may easily understand that how we can use let command for arithmetic operations.let command also used to execute one or more arithmetic expressions. It is usually used to assign values to arithmetic variables. you just need to provide space between expression like in below example.
#let a=12 b=24 c=a+b; echo $c 36
On Basis on let features, there is calculator code float over internet on various website.
#!/bin/bash if [ $# -lt 1 ]; then exit else let x="$*"; echo "$*=$x"; fi ===================================== OutPut #./mycalc 2**12 2**12=4096 #./mycalc 2**24 2**24=16777216 #./mycalc 512/64 512/64=8 #./mycalc 83468543+8236782-7823482 83468543+8236782-7823482=83881843