Get the current date
date
date +"%Y-%m-%d_%H:%M:%S"
Determining the relative path to a script's external assets (such as other bash files, CSVs, etc). This is useful to allow a script to be executed from any location in the filesystem and have it figure out where itself is located.
No matter where the following script is executed from within the filesystem, it should output the contents of the directory in which the script itself resides.
#!/bin/bash
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd $parent_path
ls -l
To check the return code of a command using an IF statement
#!/bin/bash
if systemctl restart nginx.service; then
echo "NGINX has restarted cleanly"
else
echo "NGINX failed to restart!"
fi