Batch Script – Environment Variables
Last Updated :
05 Feb, 2023
Environment variables are a collection of dynamic named values that are saved on the system and utilized by programs that are running in shells or subshells on Linux and Unix-based systems. An environment variable is, to put it simply, a variable with a name and a corresponding value.
You can alter the system’s operation and the actions of its programs by using environment variables. For instance, the environment variable may keep track of the default text editor or browser, the location of executable files, or the keyboard layout and system locale.
Environment Variables and Shell Variables
Variables are formatted as follows:
KEY=value
KEY="Some other value"
KEY=value1:value2
- When naming the variables, the case matters. By convention, environment variables’ names must be in UPPER CASE.
- When assigning values to the variable, the colon: the character must be used to separate the values.
- There is no white space around the equals = sign.
The two fundamental groups into which variables can be separated are the environment and the shell.
All child processes and shells that are launched inherit any environment variables that are system-wide and available.
Only the current shell instance is affected by shell variables. There are specific internal shell variables for each shell, including zsh and bash.
In Linux, a number of commands are available to list and set environment variables, including the following:
- Using the command env, you can execute a different program in a unique environment without changing the one that is already in use. If it is used without an argument, a list of the current environment variables will be printed.
- The command printenv prints every environment variable or just the ones that are supplied.
- Using the set command, shell variables can be set or unset. Without an argument, it will display a list of all variables, including shell and environment variables as well as shell functions.
- Delete shell and environment variables with the unset command.
- Environment variables are set with the command export.
Printing Environment Variables
In order to display environment variables printenv is the most used command that serves the purpose. Only that variable’s value is displayed, if its name is supplied as an argument to the command. printenv prints a list of all environment variables, one variable per line, if no argument is given.
For instance, you would execute: to display the value of the HOME environment variable.
For instance, you would execute: to display the value of the PATH environment variable.
For instance, running the printenv or env command without any arguments will display a list of all environment variables:
Some of the most prevalent environmental variables are listed below:
- USER – The user who is currently logged in.
- HOME – The current user’s home directory.
- EDITOR – The file editor that will be used by default. When you type edit in your terminal, this is the editor that will be used.
- SHELL – The current user’s shell path, such as bash or zsh.
- LOGNAME – The current user’s name.
- PATH – A list of folders that will be searched when commands are executed. When you run a command, the system will scan those directories in the order listed above and utilize the first executable found.
- LANG – The current locale configuration.
- TERM – Terminal emulation at the moment.
- MAIL – The location of the current user’s email.
Only the environment variables are printed using the printenv and env commands.
Other Methods to List Linux Environment Variables
The following command will display shell and environment variables using the built-in shell utility declare:-
declare
Using the set command, you may retrieve a list of every variable, including environment, shell variables, and shell functions:
set
You should most likely pipe the output to the less command because the command will output a lengthy list of all variables.
Persistent Environment Variables
Environment variables must be defined in the bash configuration files if you want them to be persistent. In most Linux systems, when a new session is launched, environment variables are read from the following files:
- The system-wide environment variables are made to be set using the /etc/environment file. Following is the format for the same:
$ FOO=bar
$ VAR _A="GeeksforGeeks"
- The variables set in this file are loaded in the /etc/profile each time a bash login shell is used. Declare environment variables in this file using the export command:
$ export JAVA_HOME="/path/to/java/home"
$ export PATH=$PATH:$JAVA_HOME/bin
- Shell configuration files specific to each user. For instance, if you’re using Bash, you can define the variables in the /.bashrc file:
$ export PATH="$HOME/bin:$PATH"
You can add additional environment variables to the running shell session by using the source command:
$ source ~/.bashrc
Setting Shell and Environment Variables
Using the name VAR_A and the value Welcome, you can easily create a new shell variable by typing:
Creating Shell Variables
Let’s start by setting up a shell variable for the active session. To accomplish this, all we need to do is provide a name and a value. We’ll stick to tradition and set the variable to a simple string while maintaining the variable name in all caps.
Now there is a shell variable. Despite being present in our current session, this variable won’t be shared with any child processes.
In the set output, we can observe this by grepping for our new variable:
By performing the identical thing with printenv, we can confirm that this is not an environmental variable:
There should be no output produced.
Creating Environmental Variables
Let’s now convert the shell variable to an environmental variable. Exporting the variable will allow us to achieve this. The appropriate command to do so is named as follows:
As a result, our variable will now be an environment variable. This can be verified by rechecking our environmental listing with printenv command.
Demoting and Unsetting Variables
We continue to declare our VAR_A variable as an environmental variable. By typing the following, we can turn it back into a shell variable:
However, it is still a shell variable:
The unset command can be used to totally unset a variable, whether it be shell or environmental:
The variable was unset, thus nothing is returned.
Batch Script of Environment Variables
Following is the batch script of environment variables: PATH, HOME, LANG, and SHELL.
Script:
#!/bin/bash
echo $PATH
echo $HOME
echo $LANG
echo $SHELL
Output:

Similar Reads
Batch Script - Working with Environment Variables
Environment Variables refer to the variables which are globally declared and can be accessed by the processor under the management of OS like Windows, Mac, and Linux. In starting they were meant to store the path locations but now they also work with Batch Script. The data of batch programs like inp
2 min read
Environment Variables in Cypress
Cypress is a modern front-end test instrument. One can write strong, reliable end-to-end tests using Cypress which should ensure that applications are running smoothly and securely. A vital aspect of the flexibility and maintainability of Cypress tests lies in its utilization of environment variable
11 min read
Next.js Environment Variables
In this article, we are going to see how to use environment variables in Next.js. Environment variables in Next.js are a way to set configuration values that are used by your application. They can be used to store data such as the name of your company, the port your application will run on, or any o
2 min read
Next.js Environment Variables
Environment variables are a fundamental aspect of modern web development, allowing developers to configure applications based on the environment they are running in (development, testing, production, etc.). In Next.js, environment variables provide a flexible and secure way to manage configuration s
3 min read
Environment Variables in Java
In Java, Environment variables are widely used by operating systems to deliver configuration data to applications. Environment variables are key/value pairs with both the key and the value being strings, similar to properties in the Java platform. What are Environment Variables in Java?In Java, Envi
5 min read
Edit and set environment variables in Postman
In Postman, the "environment" refers to a set of key-value pairs or a collection of variables used to customize requests. Environments allow you to define variables that can be reused across multiple requests, making it easier to manage different configurations for testing APIs or web services. Feat
2 min read
Access environment variable values in Python
An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python's we can use the OS module which provides a property called environ that contains environment variables in key-va
3 min read
Set environment variable in MATLAB
Setting environment variables with the help of setenv() function. The setenv() function is used to set the specified value of an operating system environment variable. Here the environment variable "name" will be replaced with "value", where "name" and "value" is its parameters. Syntaxsetenv(name, v
1 min read
How to add environment variable in MacOS?
While programmers work on different Programming Languages, they use to declare Variables to work on any certain piece of code. The Variables in general terms are used to store some values that can be again accessed in the future. There are some Operating System Variables present that are known as En
3 min read
Environment Variables in Linux/Unix
Environment variables, often referred to as ENVs, are dynamic values that wield significant influence over the behavior of programs and processes in the Linux operating system. These variables serve as a means to convey essential information to software and shape how they interact with the environme
6 min read