/ #bash #deployment 

Bash Cheatsheet: check if environment variables are set or file/symlinks exists + more

A bash scripting cheat sheet for developers who just want to get by.

Writing setup, CI and deployment flows means a bit of the old bash scripting.

Despite my deep interest in the intricacies of Bash (/sarcasm), I’ve kept hitting up Google and StackOverflow for solutions to the same couple of situations.

To avoid having to do this again myself and for your reading pleasure, here they are.

To be dangerous in terms of setup, CI and depoyment flows we will encounter the following:

Table of Contents

This was sent out on the Code with Hugo newsletter. Subscribe to get the latest posts right in your inbox (before anyone else).

Follow bash best-practices

These examples were compiled from Best Practices for Writing Bash Scripts and Use the Unofficial Bash Strict Mode (Unless You Looove Debugging).

Another resource of note: Bash 3 Boilerplate.

As a single snippet, start your bash script with:

set -euo pipefail

set -e or -o errexit - exit when a command fails

Add the following at the start of your script. It will make the script exit when a command fails.

set -e

set -u or -o nounset - exit when trying to use undefined variable

Add the following at the start of your script. It will make the script exit when attempting to use an undefined variable.

set -u

set -o pipefail - return the exit code of piped commands that error

Add the following at the start of your script.

It will return the exit code of the last command to exit with a non-zero status code (ie. error).

set -o pipefail

Debug with set -x or -o xtrace

Add the following to print out each commans befoee it’s executed.

set -x

Check if a file exists in bash

if [ ! -f ./pdfgen/pdfgen ]; then
    echo "Building pdfgen binary"
    npm run --prefix pdfgen build:linux
    echo "Pdfgen binary already exists, skipping build"
if [ ! -L /usr/local/bin/heroku ];
    wget https://cli-assets.heroku.com/branches/stable/heroku-linux-amd64.tar.gz
    sudo mkdir -p /usr/local/lib /usr/local/bin
    sudo tar -xvzf heroku-linux-amd64.tar.gz -C /usr/local/lib
    sudo ln -s /usr/local/lib/heroku/bin/heroku /usr/local/bin/heroku

Check if an environment variable is set in bash

# long
if [[ -z "${CIRCLE_BRANCH}"] ]; then
    npm run redis-cli flushall
npm run sync

# one-liner
[-z "${CIRCLE_BRANCH}"] && npm run redis-cli flushall; npm run sync

Switch over an environment variable in bash

        export ENVIRONMENT="dev"
        export HEROKU_APP=dev-app
        export ENVIRONMENT="staging"
        export HEROKU_APP=staging-app
        export ENVIRONMENT="production"
        export HEROKU_APP=production-app

Prompt the user in bash

read -p "Are you sure you want to merge 'develop' into 'staging'? (y/N)" -n 1 -r
echo # we like having a new line

if [[ $REPLY =~ ^[Yy]$ ]]
  git merge develop --ff-only
  git push

A final bit of advice, if it’s more than a couple of lines, try to use something like JavaScript, or Python to write your script.

I’ve got some resources to do that in modern JavaScript/Node:

Inject .env into your bash session/environment

We’ve got .env files laying around, Docker Compose deals with this for use usually but say we want to get something running outside of Docker (and without using something like dotenv).

Here’s the snippet for a *NIX shell:

export $(cat .env | xargs)

unsplash-logoGuilherme Cunha

Looking for a new job? Take Triplebyte’s quiz and have top tech companies pitch you!


Hugo Di Francesco

A Software Engineer who is big on Node.js, queues and Vue(s). Co-author of "Professional JavaScript" with Packt. He shares practical JavaScript tips for the developer who wants to get things done on Code with Hugo. University College London (UCL), MEng Mathematical Computation Graduate.

Get Testing Superpowers with these Underused Jest Features

Subscribe for free resources that turbocharge your Jest tests and a discount on the "Advanced Jest Handbook"