**These are gonna be more crude notes, more scattered and unorganized.**

Array Cheat Table

Example Description
declare -a array Declare an Indexed array
declare -A array Declare an Associative array
declare -a array=() Declare an indexed array with empty array
array=() create an empty array with declaring is valid
array=(1 6 3) Initialize array with numbers
array=(one two three) Initialize the array with string
array=(one two 1) Initialize the array with mixed data
${array[0]} Get first element
${array[1]} Get Second element
${array[-1]} Get Last element
${array[@]} Get All elements
${array[*]} Get All elements
${!array[!]} Get All indexes
${#array[!]} Array length
array[0]=12 Add element to array at first position.i.e index=0
array[-1]=22 Add element to array at last position.
array+=(11) Append value to an array
${array[@]:k:i} Get index=1 element starting from index=k

Numbers an MATH

let Keyword

Let is a built in function that allows for arithmetic expressions ~~~ let ~~~

Example ~~~ let x=9+10 echo $a

—Output—

19 ~~~

BASH also lets us do increments like in C languages.

expr Keyword

expr is pretty much the same a let but instead of holding the numbers in a variable, it just evaluates the given expression and prints. ~~~ expr ~~~

Example ~~~ expr 5 + 4 //output: 9

expr “5 + 4” //output: 9

expr 5+4 //output 9


We can also do expressions within double parenthesis "(())"

$x=((8+8)) echo $x

—Output— 16



### IF Statements

Kind of straight forward except there's a new nuances that can be easy to overlook.

With IF statements we use brackets [] within the expression
We can also use normal operators like ">" and "<" by using double perins instead of using brackets.
**Example**

x=10

if [ x -gt 5 ] then echo Wow $x is bigger than 5! else echo Smaller number :( fi ~~~ Note the spaces inside of the expression, there must be a space between the bracket at the characters inside.

Also we must finish the if with “fi”

ALSO, the operators are all different because IF statements actually reference the “test” command. That’s insignificant for now but here is a list of the operands.

Operator Description
! EXPRESSION The EXPRESSION is false.
-n STRING The length of STRING is greater than zero.
-z STRING The lengh of STRING is zero (ie it is empty).
STRING1 = STRING2 STRING1 is equal to STRING2
STRING1 != STRING2 STRING1 is not equal to STRING2
INTEGER1 -eq INTEGER2 INTEGER1 is numerically equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is numerically greater than INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is numerically less than INTEGER2
-d FILE FILE exists and is a directory.
-e FILE FILE exists.
-r FILE FILE exists and the read permission is granted.
-s FILE FILE exists and it’s size is greater than zero (ie. it is not empty).
-w FILE FILE exists and the write permission is granted.
-x FILE FILE exists and the execute permission is granted.

We can ALSO do elif statements!

if [ $1 -ge 18 ]    //if var 1 is greater than or equal to 18
then
    echo You may go to the party.
elif [ $2 == 'yes' ]
then
    echo You may go to the party but be back before midnight.
else
    echo You may not go to the party.
fi

You can use “&&” as the AND operator, and you can use “||” as OR.

We also have access to case statements ~~~ space_free=$( df -h | awk ‘{ print $5 }’ | sort -n | tail -n 1 | sed ‘s/%//’ )

case $space_free in [1-5]) echo Plenty of disk space available ;; [6-7]) echo There could be a problem in the near future ;; 8) echo Maybe we should look at clearing out old files ;; 9) echo We could have a serious problem on our hands soon ;; *) echo Something is not quite right here ;; esac ~~~ Use esac to close a switch case

Loops

While: Works just like any other, just note the syntax. “while, do, done”

count=1
while [ $count -le 1000 ]
do 
    echo $count operations completed
    ((count++))
done

echo All operations complete

We also have Until loops. Pretty much the same thing, I would probably just use while loops. Tho, until loops seem to be a bit faster

count=1
x=100
until [ $count -gt $x ]
do
    let y=x-count
    echo Only $y operations left
    ((count++))
done

echo All operations have conluded

The classic FOR loop Its pretty simple, be good with arrays pls ~~~ list=“word1 word2 anotherword cuh”

for w in $list do echo $w done ~~~

Using Ranges is interesting as there is some unique built in functionality This one is pretty basic ~~~ for i in {1..10} //Range from 1 to 10 do echo $i done

echo all done!

—Output— 1 2 3 4 5 6 7 8 9 10 ~~~

This is where ranges are pretty cool. We can count by numbers instead of having to do some funky stuff with modulo ~~~ for i in {10..100..2} //counts from 10 to 100 by twos do echo $i done ~~~

One last loop Select loops are pretty interesting. They work as a built in select menu. I think its pretty cool.

list="Select Continue Load Quit" //Options are set to each word in the str
PS3="Select: " //Option prompt

select i in $list
do
    if [ $i == 'Quit' ]
    then
        echo Goodbye
        break
    fi
    echo You chose $i
done

echo Selection Complete!