BASH SCRIPTING------------------------- #!/bin/bash NAME="John" echo "Hello $NAME!" VARIABLES------------------------------ NAME="John" echo $NAME echo "$NAME" echo "${NAME}!" STRING QUOTES-------------------------- NAME="John" echo "Hi $NAME" #=> Hi John echo 'Hi $NAME' #=> Hi $NAME SHELL EXECUTION------------------------ echo "I'm in $(pwd)" echo "I'm in `pwd`" CONDITIONAL EXECUTION------------------ git commit && git push git commit || echo "Commit failed" FUNCTIONS------------------------------ get_name() { echo "John" } echo "You are $(get_name)" CONDITIONALS--------------------------- if [ -z "$string" ]; then echo "String is empty" elif [ -n "$string" ]; then echo "String is not empty" fi BRACE EXPANSION------------------------ echo {A,B}.js {A,B} Same as A B {A,B}.js Same as A.js B.js {1..5} Same as 1 2 3 4 5 BASICS--------------------------------- name="John" echo ${name} echo ${name/J/j} #=> "john" (substitution) echo ${name:0:2} #=> "Jo" (slicing) echo ${name::2} #=> "Jo" (slicing) echo ${name::-1} #=> "Joh" (slicing) echo ${food:-Cake} #=> $food or "Cake" length=2 echo ${name:0:length} #=> "Jo" See: Parameter expansion STR="/path/to/foo.cpp" echo ${STR%.cpp} # /path/to/foo echo ${STR%.cpp}.o # /path/to/foo.o echo ${STR##*.} # cpp (extension) echo ${STR##*/} # foo.cpp (basepath) echo ${STR#*/} # path/to/foo.cpp echo ${STR##*/} # foo.cpp echo ${STR/foo/bar} # /path/to/bar.cpp STR="Hello world" echo ${STR:6:5} # "world" echo ${STR:-5:5} # "world" SRC="/path/to/foo.cpp" BASE=${STR##*/} #=> "foo.cpp" (basepath) DIR=${SRC%$BASE} #=> "/path/to" (dirpath) LOOPS---------------------------------- BASIC FOR LOOP------------------------- for i in /etc/rc.*; do echo $i done RANGES--------------------------------- for i in {1..5}; do echo "Welcome $i" done READING LINES-------------------------- cat file.txt | while read line; do echo $line done FOREVER-------------------------------- while true; do ??? done FUNCTIONS------------------------------ DEFINING FUNCTIONS--------------------- myfunc() { echo "hello $1" } # Same as above (alternate syntax) function myfunc() { echo "hello $1" } myfunc "John" RETURNING VALUES----------------------- myfunc() { local myresult='some value' echo $myresult } result=$(myfunc) PASSING ARGUMENTS---------------------- myfunc "$argument1" "$argument2" CALLING ARGUMENTS---------------------- $# Number of arguments $* All arguments $@ All arguments, starting from first $1 First argument RAISING ERRORS------------------------- myfunc() { return 1 } if myfunc; then echo "success" else echo "failure" fi CONDITIONALS--------------------------- CONDITIONS----------------------------- [ -z STRING ] Empty string [ -n STRING ] Not empty string [ NUM -eq NUM ] Equal [ NUM -ne NUM ] Not equal [ NUM -lt NUM ] Less than [ NUM -le NUM ] Less than or equal [ NUM -gt NUM ] Greater than [ NUM -ge NUM ] Greater than or equal [[ STRING =~ STRING ]] Regexp (( NUM < NUM )) Numeric conditions [ -o noclobber ] If OPTIONNAME is enabled [ ! EXPR ] Not [ X ] && [ Y ] And [ X ] || [ Y ] Or FILE CONDITIONS------------------------ [ -e FILE ] Exists [ -r FILE ] Readable [ -h FILE ] Symlink [ -d FILE ] Directory [ -w FILE ] Writable [ -s FILE ] Size is > 0 bytes [ -f FILE ] File [ -x FILE ] Executable [ FILE1 -nt FILE2 ] 1 is more recent than 2 [ FILE1 -ot FILE2 ] 2 is more recent than 1 [ FILE1 -ef FILE2 ] Same files EXAMPLE-------------------------------- # String if [ -z "$string" ]; then echo "String is empty" elif [ -n "$string" ]; then echo "String is not empty" fi # Combinations if [ X ] && [ Y ]; then ... fi # Regex if [[ "A" =~ "." ]] if (( $a < $b )) if [ -e "file.txt" ]; then echo "file exists" fi ARRAYS--------------------------------- DEFINING ARRAYS------------------------ Fruits=('Apple' 'Banana' 'Orange') Fruits[0]="Apple" Fruits[1]="Banana" Fruits[2]="Orange" WORKING WITH ARRAYS-------------------- echo ${Fruits[0]} # Element #0 echo ${Fruits[@]} # All elements, space-separated echo ${#Fruits[@]} # Number of elements echo ${#Fruits} # String length of the 1st element echo ${#Fruits[3]} # String length of the Nth element echo ${Fruits[@]:3:2} # Range (from position 3, length 2) OPERATIONS----------------------------- Fruits=("${Fruits[@]}" "Watermelon") # Push Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match unset Fruits[2] # Remove one item Fruits=("${Fruits[@]}") # Duplicate Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate lines=(`cat "logfile"`) # Read from file ITERATION------------------------------ for i in "${arrayName[@]}"; do echo $i done AWK------------------------------------ testvalue="The ip address of this machine is $(ip a | grep inet | grep brd | awk {'print $2'})" echo $testvalue #prints out the ip address of the host. awk {'print $2'} prints out the second word in the grepped sentence. SED------------------------------------ sed 's/a/A/g' (file) #this changes all the lowercase a to uppercase A in a file for a Dutch sed cheatsheet go to martenvd.com/sed JQ-------------------------------------- #jq example: cat devops-pipelines.json | jq '.[] as $k | "\($k.varEnv)-\($k.styling[])"'