title: Set an environment variable within a shell alias
body: <p>On my system, I've got two versions of Java installed - some programs require Java 7, some require Java 8.</p>
  
  <p>Java 8 is my system default, so when I've been running the Java 7 commands, I've been using:</p>
  
  <pre><code>JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.*.jdk/Contents/Home/ \
  java_7_program
  </code></pre>
  
  <p>I want to set an alias so I can instead write</p>
  
  <pre><code>j7 java_7_program
  </code></pre>
  
  <p>I've defined:</p>
  
  <pre><code>alias j7='JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.*.jdk/Contents/Home/'
  </code></pre>
  
  <p>But then running <code>j7 java -version</code> produces:</p>
  
  <pre><code>java version "1.8.0_45"
  Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
  Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
  </code></pre>
  
  <p>The <a href="http://linux.die.net/man/1/bash" rel="noreferrer">man page</a> (search for "Aliases") states that this is done as a direct substitution. Is there a reason as to why this isn't working?</p>
  
  <p><code>bash --version</code> prints <code>GNU bash, version 4.3.42(1)-release (x86_64-apple-darwin14.5.0)</code></p>
  
  <p>A more isolated example (minus the java):</p>
  
  <pre><code>$ alias foo='BAR=baz'
  $ type foo
  foo is aliased to `BAR=baz'
  $ foo echo $BAR
  [blank line]
  </code></pre>