- shebang
- Functions
- Parsing command line arguments
- Get the current script directory
- For Loop
- Check how a script was called
#!/usr/bin/env bash
# Define a function.
function quit {
exit
}
# Call the function.
quit
# Define a function that takes a paramter.
function printarg {
echo $1
}
# Call the function with an argument.
printarg "Hello, world!"
for i in "$@"
do
case $i in
-e=*|--extension=*)
eval EXTENSION="${i#*=}"
shift # past argument=value
;;
-s=*|--searchpath=*)
eval SEARCHPATH="${i#*=}"
shift # past argument=value
;;
-l=*|--lib=*)
eval LIBPATH="${i#*=}"
shift # past argument=value
;;
--default)
DEFAULT=YES
shift # past argument with no value
;;
*)
# unknown option
;;
esac
done
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
for i in {0..5}; do
echo $i
done
# Prints out:
# 0
# 1
# 2
# 3
# 4
# 5
Check if a scipt was called vs. source'd by another script. Analagous to if __name__ == "__main__":
in Python.
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
...
fi
The above if
condition will hit if the script is called directly, i.e. ./my_script.sh
or bash my_script.sh
. However, it will not be called if the script is source'd by another script, e.g.
# contents of some_other_script.sh
source my_script.sh
echo "doing other things..."
./some_other_script.sh
# the if condition will not hit