 # Main Index: Debian Linux Magic Spells Cheat Sheet (one liners, how to, tips and tricks)

# # Maths

```Calculate the result of an expression:

# operators: + - * / %  < > = >= <= !=

echo \$(( 10+5 )) # Sum
echo \$(( 10-5 )) # Subtraction
echo \$(( 10*5 )) # Multiplication
echo \$(( 10/5 )) # Division
echo \$(( 10**2 )) # Power
echo \$(( 15%10 )) # Module

echo \$(( (1+2)*3 )) # Result = 20

echo \$(( 5>3 )) # Returns 1 (true)
echo \$(( 3>5 )) # Returns 0 (false)

# Using expr, operators must be escaped and separated by a space

expr 10 \+ 5 # Sum
expr 10 \- 5 # Subtraction
expr 10 \* 5 # Multiplication
expr 10 \/ 5 # Division
expr 15 \% 10 # Module

expr \( 1 \+ 2 \) \* 3 # Result = 20

expr 5 \> 3 # Returns 1 (true)
expr 3 \> 5 # Returns 0 (false)

Return the absolute value of a number
echo \$(( 2-3 )) | tr -d -

Show Multiplication Table:
for i in {1..9}; do for j in \$(seq 1 \$i); do echo -ne \$i\x\$j=\$((i*j))\\t; done; echo; done;

Return the prime factors of a number:
factor NUMBER
factor NUMBER1 NUMBER2 NUMBER3... # return factors for the given numbers

Return a sequence of numbers:
echo {FIRST..LAST} # return a sequence of integer numbers from FIRST to LAST, separated by a space. Numbers can be negative, and LAST can be less than FIRST
printf '%s\n' {1..10} # return a sequence of integer numbers from FIRST to LAST, each in its own line of text (separated by new lines).
printf '%s,' {1..10} | rev | cut -c 2- | rev # return a sequence of integer numbers from FIRST to LAST, comma separated (rev and cut are used to remove the comma at the end of the string)
printf '%s, ' {1..10} | rev | cut -c 3- | rev # return a sequence of integer numbers from FIRST to LAST, comma and space separated (rev and cut are used to remove the comma and space at the end of the string)
for ((i=FIRST; i<=LAST; i+=INCREMENT)); do echo \$i; done # create a sequence from FIRST up to LAST number with given INCREMENT
seq LAST # create a sequence up to LAST number (default FIRST is 1, INCREMENT is 1)
seq FIRST LAST # create a sequence from FIRST up to LAST number (default INCREMENT is 1)
seq FIRST INCREMENT LAST # create a sequence from FIRST up to LAST number with given INCREMENT. Numbers can be negative, and END can be less than START, if the INCREMENT is negative
seq -s "," FIRST INCREMENT LAST # create a sequence from FIRST up to LAST number with given INCREMENT. Numbers are separated by a comma (",") instead of a line break
seq -s "," -w FIRST INCREMENT LAST # create a sequence from FIRST up to LAST number with given INCREMENT. Numbers are separated by a comma (",") instead of a line break. Numbers with less digit than the greater number are padded with 0s (or the sign for negative numbers)
seq FIRST INCREMENT LAST | shuf # shuffle a sequence of numbers

PI:
apt-get install pi
pi N # Print the mathematical constant PI to the given number of decimal figures. Note that pi 0 returns an empty string, pi 1 returns "3.", pi 2 returns "3.1", pi 3 returns "3.14", and so on. Default parameter for pi is 100 (that is, if you enter "pi" you get the same result as "pi 100").

Random numbers:

echo \$RANDOM  # return a random number between 0 and 32767 (16-bit integer)
echo \$((RANDOM%(MAX+1-MIN)+MIN)) # return a random number between MIN and MAX
expr \( \$RANDOM \% \( MAX \+ 1 \- MIN \) \+ MIN \) # return a random number between MIN and MAX

Random UUID:
cat /proc/sys/kernel/random/uuid # generate a random UUID, different every time is invoked

Base conversion
apt-get install bc

echo 'ibase=10; obase=16; 155'|bc  # base conversion (155 from decimal to hexadecimal)
echo 'ibase=16; obase=2; F'|bc  # base conversion (F from hexadecimal to binary)
echo '2*(3+4)'|bc -l  # perform math operations

Measure conversion
apt-get install units

units  # interactive
units "1 in" "cm"  # convert 1 inch to centimeters