-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbash_arithmetic.sh
executable file
·63 lines (44 loc) · 1.16 KB
/
bash_arithmetic.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#! /usr/bin/env bash
## Learning bash arithmetic
# Using let
# default usage, with no spaces
let a=1+9 # assigns a=10
echo "a=$a"
# If spacing needed for better readability, enclose the expression between quotes
let "b = 2 + 8" # assigns b=10
echo "b=$b"
# Take values from variables
let c=$a+$b # assigns sum of a and b to c
echo "c=$c"
# No escaping needed for *
let d=$a*$b # assigns product of a and b to d
echo "d=$d"
# incr, decr
let d++ # increment d by 1 and assign to d
echo $d
# Using expr
# evaluate the expression and writes to standard output
expr 2 + 7
# multiplication operator * need to be escaped
expr 7 * 3 # syntax error
expr 7 \* 3 # evaluates to 21
# evaluate and assign
e=$(expr 2 + 7)
echo "e=$e"
# invalid expressions. printed as is without evaluating
expr 2+7 # doesn't evaluate the expression
expr "2 + 7" # doesn't evaluate the expression
# Using double parantheses
echo $((3+5)) # prints 8
# spaces are totally fine
# evaluate the expression and assign the value to a variable
f=$((4 + 4))
echo "f=$f"
# variables do not need $ when used calculating with (( ))
g=$((f * 2))
echo "g=$g"
# increment
((g--))
echo "g=$g"
((g += 5))
echo "g=$g"