Table of Contents
This tip is about the how to Set Environment Variables in Bash on Linux. So read this free guide, How to Set Environment Variables in Bash on Linux step by step. If you have query related to same article you may contact us.
How to Set Environment Variables in Bash on Linux – Guide
This tutorial will show you how to set environment variables on Ubuntu, CentOS, Red Hat, basically any Linux distribution for a single user and globally for all users. You will also learn how to list all environment variables and how to replace (delete) existing environment variables. Environment variables are often used inside the bash shell. It is also a common means of configuring services and handling web application secrets.
It is not uncommon for environment-specific information such as endpoints and passwords to be stored as environment variables on a server. They are also used to define the important directory locations for many popular packages such as JAVA_HOME for Java.
Set environment variables in Bash
The easiest way to set environment variables in Bash is to use the keyword “export” followed by the variable name, an equals sign, and the value to assign to the environment variable.
For example, to assign the value “abc” to the variable “VAR”, you would write the following command
If you want to have spaces in your value, like “my value” or “Linus Torvalds”, you will have to enclose your value in double quotes.
To display your environment variable, you must precede the variable with a dollar sign.
Likewise, you can use the “printenv” command to print the value of your environment variable.
This time, you don’t need to precede it with a dollar sign.
Setting variables using Bash interpolation
In some cases, you may need to set a specific environment variable for the output of a command on your server.
To achieve this you will need Bash interpolation, also called parameter substitution.
Say, for example, you want to store the value of your current shell instance in a variable called MYSHELL.
To set an environment variable using parameter substitution, use the keyword “export” and have the command enclosed in parentheses preceded by a dollar sign.
For example, given our previous example, if you wanted to have the environment variable “SHELL” in a new variable called “MYSHELL”, you would write
Congratulations, you have successfully created your first environment variable in Bash!
Setting permanent environment variables in Bash
When you assign a value to a variable using “export” in a shell, the changes do not persist across restarts or across other shells.
To set a permanent environment variable in Bash, you must use the export command and add it to your “.bashrc” file (if this variable is just for you) or to the /etc/environment file if you want all users to have this environment variable.
In order for the changes to apply to your current session, you will have to source your .bashrc file.
Now, you can check the value of your environment variable in each shell you want.
This variable will be created in each shell instance for the current user.
However, if you add it to the “/etc/environment” file, the environment variable will be set for all users on your system.
After providing the file, environment variables will be set for each user on your host.
Awesome, you’ve successfully set a global environment variable on your server!
Final note
I hope you like the guide How to Set Environment Variables in Bash on Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.