title: Checking if bash environment variables exist using a function
body: <p>This may have been answered in other SO posts but I don't think I can find exactly what I am looking for.</p>
  
  <p>I have a (<code>#!/usr/bin/env bash</code>) function that checks for the existence of an (environment) variable</p>
  
  <pre><code>checkForVariable() {
    if [[ -z $1 ]]; then
      echo "Error: Define $1 environment variable"
      exit 1
    fi
  }
  </code></pre>
  
  <p>but in the error message I want it to tell me <em>which</em> env variable is missing.</p>
  
  <p>If I call it using 
  <code>checkForVariable "${ENVIRONMENT_NAME}"</code></p>
  
  <p>and if <code>ENVIRONMENT_NAME</code> is not set then obviously I will get <code>Error: Define environment variable</code> which is not useful. </p>
  
  <p>How can I change my function so that I can pass <code>checkForVariable</code> a string and not a variable reference i.e.</p>
  
  <p><code>checkForVariable "ENVIRONMENT_NAME"</code></p>
  
  <p>I've tried eg. <code>if [[ -z "\$${1}" ]]; then</code> and <code>if [[ -z "${$1}" ]]; then</code> but these don't work.</p>