Skip to content

Instantly share code, notes, and snippets.

@itskingori
Last active August 21, 2017 22:01
Show Gist options
  • Save itskingori/a4a14ec23f18a4f5ad05b4ec8826f367 to your computer and use it in GitHub Desktop.
Save itskingori/a4a14ec23f18a4f5ad05b4ec8826f367 to your computer and use it in GitHub Desktop.
Testing setting environment variables in bash

Running ./test.sh gives the below output:

test1 is empty
test2 is set (empty string)
test3 is set (has string though)
test4 is not set (does not exist at all)
#!/bin/bash
set -eu
TEST1=""
if [[ "${TEST1}" != "" ]]; then
echo 'test1 is NOT empty'
else
echo 'test1 is empty'
fi
TEST2=""
if [[ -z "${TEST2+x}" ]]; then
echo 'test2 is not set'
else
echo 'test2 is set (empty string)'
fi
TEST3="something"
if [[ -z "${TEST3+x}" ]]; then
echo 'test3 is not set'
else
echo 'test3 is set (has string though)'
fi
if [[ -z "${TEST4+x}" ]]; then
echo 'test4 is not set (does not exist at all)'
else
echo 'test4 is set'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment