1. Overview

Environment variables hold information about our login session. They’re stored for the system shell used when we execute commands.

Usually, when we execute a command, we don’t manipulate the environment variables directly. However, sometimes, we want to change the system’s predefined environment variables for one or more commands.

In this quick tutorial, let’s see how to set environment variables for one or multiple commands.

2. A Shell Script Example

First of all, let’s create a simple shell script to simulate a command that works with different environment variables:

$ cat java_app_sim.sh
#!/bin/bash
date +'%A %B %e %T %Y'
echo "JAVA_HOME: $JAVA_HOME"
echo "Now, starting a Java Application with the JAVA_HOME setting, please standby ..."

The java_app_sim.sh script simulates a Java program launcher, running a Java application with the current JAVA_HOME setting.

Next, let’s execute the java_app_sim.sh script with our current settings and see what comes out:

$ ./java_app_sim.sh
Monday January 10 17:53:56 2022
JAVA_HOME: /usr/lib/jvm/default
Now, starting a Java Application with the JAVA_HOME setting, please standby ...

As shown in the output above, java_app_sim.sh prints the current date and time and the system’s default JAVA_HOME. Then it starts a Java program. Of course, we simulate it by the echo command here.

In this tutorial, we’ll first demonstrate how to set environment variables only for one single command.

Further, we’ll address how to set environment variables for all future commands in the same Bash session.

Next, let’s see them in action.

3. Setting Multiple Variables Only for a Single Command

Let’s say we have multiple Java versions installed on our system. We want to run the java_app_sim.sh application with a particular Java version instead of the default one.

Also, we would like to set the application’s language to German via setting the LANG variable.

Moreover, we want the environment variable changes only to affect the java_app_sim.sh command.

We can set variables for a single command using this syntax:

VAR1=VALUE1 VAR2=VALUE2 ... Command ARG1 ARG2...

Now, let’s set the required variables and execute our Java application launcher and see what it reports:

$ LANG=de_DE JAVA_HOME=/the/special/java ./java_app_sim.sh 
Montag Januar 10 18:06:23 2022
JAVA_HOME: /the/special/java
Now, starting a Java Application with the JAVA_HOME setting, please standby ...

We’ve set the LANG and the JAVA_HOME variables. As we can see in the output, our script has taken the customized JAVA_HOME.

Also, if we look at the date information above, it’s printed in German. So, it took the LANG value as we expected. So far, so good!

Finally, let’s examine if the two variables still hold the original values after the script’s execution:

$ echo -e "LANG: $LANG \nJAVA_HOME: $JAVA_HOME"
LANG: en_US.utf8 
JAVA_HOME: /usr/lib/jvm/default

As we can see, the two variables still have the original values.

If we execute our script once again without customizing any variables, it’ll take the default settings:

$ ./java_app_sim.sh 
Monday January 10 18:14:11 2022
JAVA_HOME: /usr/lib/jvm/default
Now, starting a Java Application with the JAVA_HOME setting, please standby ...

4. Setting Multiple Variables for All Future Commands

We’ve learned how to set variables only for one single command. This technique can be handy when we want to run a command with different environment settings temporarily.

However, sometimes, we would like to set some environment variables in the current Bash session so that all future commands running in this session will use the newly set variables.

The key to achieving that is to use the export command:

export VAR1=VALUE1
export VAR2=VALUE2
...
Command ARG1 ARG2...

We can, of course, write it in a more compact form:

export VAR1=VALUE1 VAR2=VALUE2; Command ARG1 ARG2...

Now, let’s test it with our java_app_sim.sh script:

$ export LANG=de_DE JAVA_HOME=/the/special/java; ./java_app_sim.sh
Montag Januar 10 18:49:32 2022
JAVA_HOME: /the/special/java

As we can see in the output above, our script has applied our customized variable values.

Next, let’s check if the two environment variables’ values have been changed in the same Bash session:

$ echo -e "LANG: $LANG \nJAVA_HOME: $JAVA_HOME"
LANG: de_DE 
JAVA_HOME: /the/special/java

Good, as we expected, the two variables’ values have been updated.

Now, if we start our script once again without setting any variable, it’ll take the updated variables’ values:

$ ./java_app_sim.sh 
Montag Januar 10 19:01:06 2022
JAVA_HOME: /the/special/java
Now, starting a Java Application with the JAVA_HOME setting, please standby ...

Therefore, all future commands in the same Bash session will execute with the updated LANG and JAVA_HOME variables.

5. Conclusion

In this article, we’ve learned how to set environment variables only for one single command. This is pretty convenient when we would like to execute a command with a different set of environment variables.

However, when we want to update environment variables in the current Bash session so that all commands in the same session will take the updated variables, we can use the export command. Again, we’ve addressed the trick through examples.

Comments are closed on this article!