Skip to main content

How to assign default values to variables in Bash.

#!/usr/bin/env bash

# If $VARIABLE is not set or null, set $FOO with "defaultValue"
FOO=${VARIABLE:-defaultValue}

# If $VARIABLE is not set or null, set both $FOO and $VARIABLE with "defaultValue"
FOO=${VARIABLE:=defaultValue}

# If the first command line argument "$1" is not set or null, set $FOO with "defaultValue"
FOO=${1:-defaultValue}