Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set an environment variable within a shell alias

On my system, I've got two versions of Java installed - some programs require Java 7, some require Java 8.

Java 8 is my system default, so when I've been running the Java 7 commands, I've been using:

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.*.jdk/Contents/Home/ \
java_7_program

I want to set an alias so I can instead write

j7 java_7_program

I've defined:

alias j7='JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.*.jdk/Contents/Home/'

But then running j7 java -version produces:

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)

The man page (search for "Aliases") states that this is done as a direct substitution. Is there a reason as to why this isn't working?

bash --version prints GNU bash, version 4.3.42(1)-release (x86_64-apple-darwin14.5.0)

A more isolated example (minus the java):

$ alias foo='BAR=baz'
$ type foo
foo is aliased to `BAR=baz'
$ foo echo $BAR
[blank line]
like image 646
Fabian Tamp Avatar asked Oct 27 '15 08:10

Fabian Tamp


People also ask

How do I set an environment variable in shell?

To set an environment variable everytime, use the export command in the . bashrc file (or the appropriate initialization file for your shell). To set an environment variable from a script, use the export command in the script, and then source the script. If you execute the script it will not work.

Is alias an environment variable?

So now we might find a little clearer where the differences are between aliases and environment variables, yes, they are both like keywords, but an alias holds a reference to a command and an environment variable just withholds data.

How do I set environment variables in bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.

Can you use alias in shell script?

An alias is a (usually short) name that the shell translates into another (usually longer) name or command. Aliases allow you to define new commands by substituting a string for the first token of a simple command. They are typically placed in the ~/. bashrc (bash) or ~/.


2 Answers

Answering your isolated example:

when you do something like this:

bar=foo my_command

then bar is set in my_command's environment (and is not seen by the current shell). Hence, when you do:

bar=stuff
bar=foo my_command "$bar"

since the expansion of $bar occurs before my_command is executed, then it's like doing:

bar=foo my_command stuff

since the $bar is expanded before my_command is forked. That explains the [blank line] you obtain in your example:

$ alias foo='BAR=baz'
$ type foo
foo is aliased to `BAR=baz'
$ foo echo $BAR
[blank line]

Just for fun, try these:

$ alias foo=BAR=baz
$ BAR=something
$ foo echo "$BAR"
something

makes sense?

$ alias foo=BAR=baz
$ foo eval 'echo "$BAR"'
baz

this is because BAR is passed in eval's environment, and then echo "$BAR" is expanded… with the expected value (note the single quotes!).

Similarly,

$ alias foo=BAR=baz
$ foo sh -c 'echo "$BAR"'
baz
like image 93
gniourf_gniourf Avatar answered Sep 16 '22 22:09

gniourf_gniourf


The jdk1.7.*.jdk bit is probably the problem. If I put an explicit version in it works fine

You can use /usr/libexec/java_home -v 1.7 to get the latest 1.7 version installed.

So to redo your alias, try:

alias j7='JAVA_HOME=`/usr/libexec/java_home -v 1.7`'

Note the back ticks around the java_home bit which will then execute the command to generate the correct path to set JAVA_HOME to.

like image 32
Richard Avatar answered Sep 16 '22 22:09

Richard