Redirect commands output to command > /dev/null
to suppress the output and only focus on the success or failure of the command and make sure you only include the important information
bash -n scriptname
check for script syntax errors
Abort the script on errors and unbound variables. Put the following code at the beginning of each script
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
# shorter version
set -euo pipefail
set -e
is in the script, if a command gives an error the script will stop executing as its a best practice to handle errors$?
holds the exit code status of the last command
some_command > /dev/null
if [ $? -eq 0 ]; then
echo "Command executed successfully."
else
echo "Command encountered an error."
exit 1;
fi
$@
passes parameters as separate parameters, whereas $*
just passes all parameters as a single parameter
#!/bin/bash
echo "Using \\"\\$@\\":"
for arg in "$@"; do
echo "$arg"
done
echo -e "\\nUsing \\"\\$*\\":"
for arg in "$*"; do
echo "$arg"
done
Flow controls cheat sheet
The [
and test
commands are switches, they emit true
or false
response. Example, you want to make sure some file exists, so you do test -e example && echo "it exists"
or
[ -e example ] && echo "it exists"
if [ -f /some/file ]
declare
command gives attributes to a variable and use -p
to display the attributes
declare -i num=10 # Declares num as an integer
declare -r readonly_var="This variable is read-only"
declare -x EXPORTED_VAR="This variable is exported to the environment"
declare -a my_array # Declares my_array as an indexed array
declare -A assoc_array # Declares assoc_array as an associative array
basename "/path/to/file"
command extracts the filename (which will be file
)
filename=$(basename "$string")
[@]
is a special notation in Bash to expand all elements of the array to be used. When you use "${my_array[@]}"
within double quotes in a loop or any other context, it ensures that each element of the array is treated as a separate word.
my_array=("apple" "banana" "orange with spaces")
for element in "${my_array[@]}"; do
echo "$element"
# will output orange with spaces as a single line than each word in a each line
done
set -x
and bash -x
are both ways to enable debugging output for a script or a Bash session. set -x is used in scripts like the following example
#!/bin/bash
set -x
ls -la
echo "hello"
# output will be:
+ ls -la
total 72
drwxr-xr-x 8 ibrahim ibrahim 4096 Mar 25 09:58 .
drwxr-x--- 57 ibrahim ibrahim 4096 Mar 25 09:58 ..
+ echo hello
hello
bash -x <script.sh>
will do the same thing and no need to include set -x
in the scripttrap '(read -p "[$BASH_SOURCE:$LINENO] $BASH_COMMAND")' DEBUG
at the beginning of any scriptAlways use long parameter notation when available for the script to be more readable
# Avoid:
rm -rf -- "${dir}"
# Good:
rm --recursive --force -- "${dir}"
Prefer local variables within functions over global variables, if you need global variables, make them readonly
Variables should always be quoted, especially if their value may contain a whitespace or separator character: "${var}"
or "$file"
printf
is preferable to echo
. printf
gives more control over the output, it’s more portable and its behaviour is defined better
Never use deprecated style. Most notably:
myfunc() { ... }
, not function myfunc { ... }
[[
instead of [
or test
$( ... )
then
, do
, etc on same line, not newlineUse indentation and multiple lines
az aks create \\
--resource-group my-resource-group \\
--name my-aks-cluster \\
--enable-managed-identity \\
--node-count 1 \\
--enable-addons monitoring \\
--generate-ssh-keys