Declaration:
NAME="value"VAR=valueUsage:
echo "Hello $NAME."echo "Hello ${NAME}."VAR=$((VAR + 1))function test {
VAR="$1"
echo "$VAR"
}
VAR="a"
echo "$VAR"
test "b"
echo "$VAR"
function test {
local VAR="$1"
echo "$VAR"
}
VAR="a"
echo "$VAR"
test "b"
echo "$VAR"
function runCmd {
local CMD="$1"
echo "Command: $CMD"
eval $CMD
return $?
}
runCmd "ffmpeg -i xxx ..."
function checkError {
local VALUE=$1
local MSG="$2"
if [ $VALUE -ne 0 ]; then
echo "ERROR: $MSG"
exit $VALUE
fi
}
...
RETVAL=$?
checkError $RETVAL "This went wrong."
TEXT=$(command arg1 arg2 arg3 ...)
echo "$TEXT"
TEXT=$(cat /etc/passwd)
echo "$TEXT"
TEXT=$(myFunction "$X")
echo "$TEXT"
Bash has 3 different types of comparison operators:
Check if variable matches string: if [ "$NAME" == "Dragon" ]; then ...; fi
Check if variable is empty: if [ -z "$NAME" ]; then ...; fi
Check if parameter is a folder: if [ -d "$SOURCE" ]; then ...; fi
Check if value is greater-than 1: if [ $VALUE -gt 1 ]; then ...; fi
NAME="My Name"
if [ $NAME == "My Name" ]; then ...
Throws the error: line x: [: too many arguments
Because this is what happens internally:
if [ My Name == "My Name" ], then ...
# OR = ||
if [[ "$1" == "x" || "$1" == "X" ]]; then
echo "It's an x (or an X)!"
fi
# AND = &&
if [[ $1 -gt 3 && $1 -lt 7 ]]; then
echo "Bingo!"
fi
if [ $VAL -eq 1 ]; then
# Do if VAL=1
elif [ $VAL -eq 2 ]; then
# Do if VAL=2
elif [ $VAL -eq 3 ]; then
# Do if VAL=3
else
# Do if anything else.
fi
Multiple choice tests ;)
case "$CHOICE" in
one)
...
;;
two)
...
;;
*)
...
;;
esac
And they both allow to return a numeric value, stored in $?
function isDir() {
if [ -d "$1" ]; then
# 0 = true
return 0
else
# not 0 = false
return 1
fi
}
DIR="$1"
echo "Is $DIR a folder...?"
if isDir "$DIR"; then echo "Yes!"; else echo "No."; fi
${#string} Length of $string
${string:position} Extract substring from $string at $position
${string:position:length} Extract $length characters substring from $string at $position
${string#substring} Strip shortest match of $substring from front of $string
${string##substring} Strip longest match of $substring from front of $string
${string%substring} Strip shortest match of $substring from back of $string
${string%%substring} Strip longest match of $substring from back of $string
${string/substring/replacement} Replace first match of $substring with $replacement
${string//substring/replacement} Replace all matches of $substring with $replacement
${string/#substring/replacement} If $substring matches front end of $string, substitute $replacement for $substring
${string/%substring/replacement} If $substring matches back end of $string, substitute $replacement for $substring
See “Advanced Bash-Scripting Guide (tldp.org)” for additional information.
Extract fixed length:
LINE=$(md5sum "$0")
HASH=${LINE:0:32}
echo "Line: '$LINE'"
echo "Hash: '$HASH'"
FILE="bla.txt"
NAME="${FILE##*.}"
echo $NAME
> ${string##substring} Strip longest match of $substring from front of $string
Take 1 image file and copy it n times with subsequent filenames, according to a given naming rule.
Take 1 image file and copy it n times with subsequent filenames, according to a given naming rule.
For loop with “seq”: for i in $(seq 1 $n); do ... done
While loop: while [ $i -le $n ]; do ... done
Write a function that takes an integer as input and increases its value by 1 and returns this number. Echo the incremented value.
Optional: Use this function as in a while loop to count up until 13.
Write a function that you can use to check if a given string has the following properties:
Return True if the string matches these conditions, False if otherwise.