Bash is a shell scripting language included on most Linux and Mac computers. It’s useful for writing small scripts that don’t require another software language to be installed and are immediately portable. Good places to use bash scripts are when you want to write a cronjob, backup some files, or run an update task. Here are the basics to get you started.
Start your script with She-Bang header:
Variables are assigned by a key-value pairing:
You can print to the console by using echo:
An IF statement:
if [ ! -s $FILENAME ] ; then
The bang ! negates the result, and the -s parameter checks if a file exists.
Example of a function:
# function contents here
# the hash symbol makes the rest of this line a comment
Call this function by using its name:
You can pass a variable to a function:
And use it within the function by reference to $1