How to Set Environment Variables in Linux

This tutorial is about How to Set Environment Variables in Linux. We will try our best so that you understand this guide. I hope you like this blog, How to Set Environment Variables in Linux. If your answer is yes, please do share after reading this.

So lets keep reading for intertesting info:

Check How to Set Environment Variables in Linux

An environment variable is any variable that can be accessed by any process on a computer. In a nutshell, they are simply variables that can be used in the shell or command line and its threads. So for example you may have installed Anaconda and you may need to set a global environment variable called “PATH” in your bash_profile. Even if you didn’t set it, the variable may have been added automatically.

Have you seen programmers working on those black (sometimes green) screens all day and wondered what magic is going on? These green screens are called terminal/shell/bash and have now become an integral part of every machine learning engineer, data scientist, and programmer’s life. We use Linux shells for a variety of tasks: logging into servers, writing complex scripts, performing various automations, and running various programs.

Sometimes we may need to save information about the default text editors or the paths where Python or Java are stored so that they can be accessed by all processes on your system. In such cases, you may have seen the use of commands like export and unset in the Linux shell. These commands are used to set and disable what is called an environment variable. In this article, we will teach you how to set environment variables in Linux.

How to set environment variables in Linux

Set an environment variable

  • The export command is used to set the environment variable. We give the variable a name that is used to access it in shell scripts and configurations, and then a value to store the necessary data in the variable.
  • For example, to set the environment variable for the home directory of a manual installation of OpenJDK 11, we would use something similar to the following.
    • export JAVA_HOME=/opt/openjdk11
  • To generate the value of the environment variable from the shell, we use the echo command and prefix the variable name with a dollar sign ($).
  • And as long as the variable has a value, it will iterate. If no value is set, an empty line will be displayed instead.

Unset an environment variable

To reset an environment variable, which completely removes its existence, we use the unset command. Simply replacing an environment variable with an empty string will not remove it, and in most cases is likely to cause problems with scripts or applications that expect a valid value.

  • The following syntax is used to unset an environment variable
  • For example, to disable the JAVA_HOME environment variable, we would use the following command.

List of all environment variables set

  • To list all the environment variables, we simply use the set command without any arguments.
  • An example of the output would be similar to the following, which has been truncated for brevity.
    • BASH=/bin/bash
    • BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:inter
    • active_comments:login_shell:progcomp:promptvars:sourcepath
    • BASH_ALIASES=()
    • BASH_ARGC=([0]=”0″)
    • BASH_ARGV=()
    • BASH_CMDS=()
    • BASH_COMPLETION_VERSINFO=([0]=”2″ [1]=”8″)
    • BASH_LINENO=()
    • BASH_SOURCE=()
    • BASH_VERSINFO=([0]=”5″ [1]=”0″ [2]=”3″ [3]=”1″ [4]=”release” [5]=”x86_64-pc-linux-gnu”)
    • BASH_VERSION=’5.0.3(1)-release’
    • COLUMNS=208
    • STACK DIR=()
    • IDUE=1000
    • GROUPS=()
    • HISTCONTROL=ignore both
    • HISTFILE=/home/ubuntu/.bash_history
    • HISTFILESIZE=2000
    • HISIZE=1000
    • HOME=/home/ubuntu
    • HOSTNAME = ubuntu1904
    • HOSTTYPE=x86_64
    • IFS=$’tn’
    • LANG=en_US.UTF-8
    • MENOSCLOSE=’/usr/bin/lesspipe %s %s’
    • LESSOPEN=’| /usr/bin/lesspipe %s’
    • LINES=54

Persistent environment variables for a user

When an environment variable is set from the shell using the export command, its existence ends when the user’s session ends. This is problematic when we need the variable to persist between sessions.

  • Open the current user’s profile in a text editor
  • Add the export command for each environment variable you want to keep.
    • export JAVA_HOME=/opt/openjdk11
  • Save your changes.
  • Adding the environment variable only to a user’s bash profile will not automatically export it. However, the variable will be exported the next time the user logs in. To immediately apply all changes to bash_profile, use the source command.

Export environment variable

Export is a built-in shell command for Bash used to export an environment variable to allow new child processes to inherit it.

  • To export an environment variable, run the export command while setting the variable.
    • export MYVAR=”my variable value”
  • We can see a full list of exported environment variables by running the export command without any arguments.
    • export
    • SHELL=/bin/zsh
    • SHLVL=1
    • SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.1pB5Pry8Id/Listeners
    • TERM=xterm-256color
    • TERM_PROGRAM=code vs
    • TERM_PROGRAM_VERSION=1.48.2
  • To see all variables exported in the current shell, use the -p flag with export.

Setting permanent global environment variables for all users

You can create a persistent environment variable that persists across reboots by adding it to the default profile. All system users load this profile, including service accounts. All global profile settings are stored in /etc/profile. While this file can be edited in a directory, it is actually recommended that you store global environment variables in a directory called /etc/profile.d, where you will find a list of files used to set system-wide environment variables.

  • Create a new file in /etc/profile.d to store the global environment variables. The name of the must be contextual so that others can understand its purpose. For demonstration purposes, we will create a permanent environment variable for HTTP_PROXY.
    • sudo touch /etc/profile.d/http_proxy.sh
  • Open the default profile in a text editor.
    • sudo vi /etc/profile.d/http_proxy.sh
  • Add new lines to export the environment variables.
    • export HTTP_PROXY=http://my.proxy:8080
    • export HTTPS_PROXY=https://my.proxy:8080
    • export NO_PROXY=localhost,::1,.example.com
  • Save your changes and exit the text editor.

Final words: How to Set Environment Variables in Linux

I hope you understand this article, How to Set Environment Variables in Linux. If your answer is no, you can ask anything via the contact forum section related to this article. And if your answer is yes, please share this article with your friends and family to give us your support.

Editorial Staff
Editorial Staffhttps://www.bollyinside.com
The Bollyinside editorial staff is made up of tech experts with more than 10 years of experience Led by Sumit Chauhan. We started in 2014 and now Bollyinside is a leading tech resource, offering everything from product reviews and tech guides to marketing tips. Think of us as your go-to tech encyclopedia!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related Articles

Best Telemedicine Software: for your healthcare practice

Telemedicine software has transformed my healthcare visits. It's fantastic for patients and doctors since they can obtain aid quickly. I...
Read more
I love microlearning Platforms in today's fast-paced world. Short, focused teachings that engage me are key. Microlearning platforms are great...
Think of a notebook on your computer or tablet that can be changed to fit whatever you want to write...
As of late, Homeschool Apps has gained a lot of popularity, which means that an increasing number of...
From what I've seen, HelpDesk software is essential for modern businesses to run easily. It's especially useful for improving customer...
For all of our important pictures, stories, and drawings, Google Drive is like a big toy box. But sometimes the...