Bash Script Snippets

Last Updated: Apr. 12th 2022 at 5:28am

Ensure root user

This will stop a script from executing if the root user isn't running it.


#check if root
if [ "$(id -u)" != "0" ]; then
  echo "This script must be run as root" <&
  exit;
fi

Checks if usid is 0, if not exists.

Import an dotenv file

Stores information from a dotenv file to bash variables


export $(grep -v '^#' .env | xargs)