Skip to content

Instantly share code, notes, and snippets.

@estysdesu
Last active July 21, 2019 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estysdesu/50ecd5146a5ca9ea1e40eb07849aac95 to your computer and use it in GitHub Desktop.
Save estysdesu/50ecd5146a5ca9ea1e40eb07849aac95 to your computer and use it in GitHub Desktop.
[Shell: math constructs] #bash #math #double #parenthesis #(( #sh #seq #bc #range
##### `((` (DOUBLE PARENTHESIS) #####
# Bash only
# arithmetic expansion
(( a=25 )) # C style setting
(( a++ )) && echo $a # a == 26; same as `(( ++a ))`
(( a-- )) && echo $a # a == 25; same as `(( --a ))`
# arithmetic evaluation
(( t = a<45?7:11 )) # t == 7; if a < 45, then t = 7, else t = 11
# https://www.tldp.org/LDP/abs/html/loops1.html#FORLOOPC
LIMIT=10
for ((a=1; a <= LIMIT ; a++)) # `for ((a=1, b=1; a <= LIMIT ; a++, b++))`
do
echo -n "$a " # `echo -n "$a-$b "`
done
##### seq (SEQUENCE) #####
# https://www.tldp.org/LDP/abs/html/loops1.html#FORLOOPC
seq 10 # sequence 1 to 10
seq -10 0 # sequence -10 to 0
##### `{<#>..<#>}` (BRACE EXPANSION W/ RANGE) #####
# https://www.tldp.org/LDP/abs/html/loops1.html#FORLOOPC
echo {1..10} # list 1-10
##### bc (BASIC CALCULATOR) #####
# -l: math library (mathlib)
# -i: interactive
# scale: allows setting scale (precision/decimal places)
# s: sine
# c: cosine
# a: arctangent
# l: natural logarithm
# e: exponential
pi=$(echo "scale=10; 4*a(1)" | bc -l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment