Shell Syntax
Introduction
start with a she bang
#!/bin/bash
#!/usr/bin/env bash
Printing
echo hi
echo "hello"
a="1"
echo $a
Variables
a="this is it"
a = "sdaf" # this is wrong no space
Parameter expansion
a="a string"
echo "${a}"
echo "${a/string/strings/}" # replace string with strings
echo ${a:0:3} # print form 0, and then 3 characters
echo ${a:-5} # return last five character form the string
echo ${#a} # the length of the string
echo ${a:-"or print this is a is missing or empty"}
Integer
a=7
Arrays
array0=(one two three four five six)
echo $array # one
echo ${array0[0]} # one
echo ${array0[1]} # two
echo ${array0[@]} # entire array
echo ${#array0[1]} # 3 length of two
echo #{array0[@]:3:2} # print from 3rd index and two elements
# print the array
for i in "${array0[@]}";do
    echo "$i"
done
brace expansion
echo {1..10} # 1 2 3 4 5 6 7 8 9 10
echo {a..z}
built in variables
$?- last program return value$$- script pid$#- number of argument passed to script$@- all arguments passed to script$1- first argument,$2- second argument, and so on...
run external command
echo "$(pwd)"
input
echo "enter amount: "
read amount
echo "amount: $amount"
flow control
if else
if [ $amount == 1000 ];then
    echo "expensive"
fi
if [ $amount == 1000 ];then
    echo "expensive"
else
    echo "cheap"
fi
if [ $amount == 1000 ];then
    echo "expensive"
elif [ $amount == 500 ];then
    echo "nice"
else
    echo "cheap"
fi
echo "Always executed" || echo "Only executed if first command fails"
echo "Always executed" && echo "Only executed if first command does NOT fail"
conditionals
-a file-Trueiffileexists.-b file-Trueiffileexists and is a block special file.-c file-Trueiffileexists and is a character special file.-d file-Trueiffileexists and is a directory.-e file-Trueiffileexists.-f file-Trueiffileexists and is a regular file.-g file-Trueiffileexists and its set-group-id bit is set.-h file-Trueiffileexists and is a symbolic link.-k file-Trueiffileexists and its "sticky" bit is set.-p file-Trueiffileexists and is a named pipe (FIFO).-r file-Trueiffileexists and is readable.-s file-Trueiffileexists and has a size greater than zero.-t fd-Trueiffiledescriptorfdis open and refers to a terminal.-u file-Trueiffileexists and its set-user-id bit is set.-w file-Trueiffileexists and is writable.-x file-Trueiffileexists and is executable.-G file-Trueif file exists and is owned by the effective group id.-L file-Trueif file exists and is a symbolic link.-N file-Trueif file exists and has been modified since it was last read.-O file-Trueif file exists and is owned by the effective user id.-S file-Trueif file exists and is a socket.file1 -ef file2Trueiffile1andfile2refer to the same device and inode numbers.
file1 -nt file2-Trueiffile1is newer (according to modification date) than file2, or if file1 exists and file2 does not.
file1 -ot file2Trueiffile1is older than file2, or if file2 exists and file1 does not.
-o optname- True if the shell option optname is enabled. The list of options appears in the description of the -o option to the set builtin.-v varname- True if the shell variable varname is set.-R varname- True if the shell variable varname is set and is a name reference.-z string- True if the length of string is zero.-n stringorstringTrue if the length of string is non-zero.string1 == string2orstring1 = string2- True if the strings are equal. When used with the[[command, this performs pattern matching.=should be used with the test command for POSIX conformance.string1 != string2True if the strings are not equal.string1 < string2True if string1 sorts before string2 lexicographically.string1 > string2True if string1 sorts after string2 lexicographically.arg1 OP arg2OP is one of-eq, equal to-ne, not equal to-lt, less than-le, less than or equal to-gt, greater than-ge. greater than or equal to- When used with the 
[[command,Arg1andArg2are evaluated as arithmetic expressions. 
and, or
if [ "$Name" == "Steve" ] && [ "$Age" -eq 15 ]
then
    echo "This will run if $Name is Steve AND $Age is 15."
fi
if [ "$Name" == "Daniya" ] || [ "$Name" == "Zach" ]
then
    echo "This will run if $Name is Daniya OR Zach."
fi
regex
~=is used
Email=me@example.com
if [[ "$Email" =~ [a-z]+@[a-z]{2,}\.(com|net|org) ]]
then
    echo "Valid email!"
fi
case
case "$Variable" in
    # List patterns for the conditions you want to meet
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;  # match everything
esac
run command in background
sleep 30 &
- how to check the running background jobs use 
jobscommand - to bring a job to foreground 
fg pid ctrl+cto kill a processctrl+zto pause a processbgresume process in bg which have been paused- 
kill %2kill job 2 - 
run a command and print its file descriptor
echo <(echo "#helloworld")
 
expressions
echo $(( 10 + 5 )) # 15
id++id--variable post-increment and post-decrement++id--idvariable pre-increment and pre-decrement-+unary minus and plus!~logical and bitwise negation**exponentiation*/%multiplication, division, remainder+-addition, subtraction<<>>left and right bitwise shifts<=>=<>comparison==!=equality and inequality&bitwise AND^bitwise exclusive OR|bitwise OR&&logical AND||logical ORexpr ? expr : exprconditional operator=*=/=%=+=-=<<=>>=&=^=|=assignmentexpr1 , expr2comma
loops
for Variable in {1..3}
do
    echo "$Variable"
done
for ((a=1; a <= 3; a++))
do
    echo $a
done
for Variable in file1 file2
do
    cat "$Variable"
done
for Output in $(ls)
do
    cat "$Output"
done
while [ true ]
do
    echo "loop body here..."
    break
done
functions
function foo ()
{
    echo "Arguments work just like script arguments: $@"
    echo "And: $1 $2..."
    echo "This is a function"
    returnValue=0    # Variable values can be returned
    return $returnValue
}
exit shell script
- do the cleanup, remove temporary files you created
 - The 
trapcommand allows you to execute a command whenever your script receives a signal. - Here, 
trapwill executermif it receives any of the three listed signals. 
trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM