Now Reading
Linux Environment Variables: How to Set Them
[vc_row thb_full_width=”true” thb_row_padding=”true” thb_column_padding=”true” css=”.vc_custom_1608290870297{background-color: #ffffff !important;}”][vc_column][vc_row_inner][vc_column_inner][vc_empty_space height=”20px”][thb_postcarousel style=”style3″ navigation=”true” infinite=”” source=”size:6|post_type:post”][vc_empty_space height=”20px”][/vc_column_inner][/vc_row_inner][/vc_column][/vc_row]

Linux Environment Variables: How to Set Them

Linux featured
Linux featured

As you customize and configure your Linux system, you’ll need to know how to set environment variables. Let us show you how it works.

If you’re using a PC, the operating system needs to know where to locate certain files or how to present the time and date. Windows usually does this by using configuration files and the Registry.

This setting is used on Unix-based Linux systems, such as Linux. Environment variables. This guide will show you how to set Linux environment variables.

Environment and Shell Variables in Linux

There are two types of these variables—environmentAnd shell. The environment variable has an effect on the whole system, whether it is used in a graphical interface or on the command line. These variables store things like the default web browser or text editor, the path to executables files, the system locale, and keyboard layout.

A shell variable has a smaller scope. It is non-persistent and applies only to the current shell or command line instance. There are many shell environments. zshAnd bash, each have their own set if internal shell variables.

The format is what they share in common. Each environment and every shell variable will follow one or more of these basic formats.

KEY=value
KEY="Some value, including spaces"
KEY=value1:value2

When working with shell variables and environment variables, keep these things in mind:

  • It is important to consider the case. Environment variables should always be in uppercase
  • You should not put a space between the variable names. =symbol and the value
  • If a variable contains multiple values, they must all be separated by a colon:) character.

Linux Environment Variables: How to List and Configure

There are many commands that can be used to list, set, or unset environment variables in Linux. These commands include:

  • env: This command allows you to run another application in a custom environment, without having to modify the current one. It prints a list of current environment variables without any arguments.
  • Printenv: If you wish to print all or a particular environment variable, Printenv is quite useful.
  • SetThis command can be used to set or remove shell variables. If you don’t have an argument, SetThis will print a list containing all variables, including environment variables and shell variables, as well as the shell functions.
  • Unset: The command that deletes environment variables and shell variables.
  • Export: This command is what you’ll use most often to set environment variables.

Working with Environment Variables

Before you change your environment variables, make sure to check your current configurations. You can use PrintenvThis will allow you to find out which environment and shell variables are already present in your Linux system.

Run the following to see all current environment variables PrintenvWithout arguments, you can simply type the command. Warning! The output can be quite lengthy.

To display, for example, the value of HOMEVariable, you would use the following.

jeff@UbuntuVM $ printenv HOME
/home/jeff
jeff@UbuntuVM:~$

You can also offer more than one argument PrintenvTo check multiple variables at once.

jeff@UbuntuVM:~/documents$ printenv LANG PWD
en_US
/home/jeff/documents
jeff@UbuntuVM:~/documents$

These are some of the most common environmental variables that you will encounter:

  • USER: The current logged into user.
  • HOME: The current user’s home directory.
  • EDITOR: The default text editor if you type Editat the command line.
  • SHELL: The current shell of the user, like bashOr zsh.
  • PATH: A list containing directories to be searched whenever you execute a command. Linux searches these directories using the first match it finds.

Change or Set Up New Environment Variables

There are two steps you’ll need to follow to set an environment variable on Linux. First, define it as a shell variable. Next, you will export it as an environment variable. This command sets a variable called “Name” in the environment. MY_VARIt is described as groovy.

jeff@UbuntuVM:~$ MY_VAR='groovy'
jeff@UbuntuVM:~$ 

This confirmation will not be visible, but you can confirm if it was.

jeff@UbuntuVM:~$ echo $MY_VAR
groovy
jeff@UbuntuVM:~$ 

Issue the command printenv MY_VARAt this point, there shouldn’t be any output. This is because there isnt an environment variable named MY_VAR. To change that, you would use the Export command.

jeff@UbuntuVM - $ export MY_VAR
jeff@UbuntuVM:~$ 

Now, printenv MY_VARDifferent output should be used

jeff@UbuntuVM $ printenv MY_VAR
groovy
jeff@UbuntuVM:~$ 

There is another way. Use this command to set environment variables in one line.

jeff@UbuntuVM
jeff@UbuntuVM:~$ 

It is important to remember that the environment variables you have set in this way only affect the current session. The environment variables will be lost if you log out or create a new shell.

Linux: Setting Environment Variables to Be Persistent

If you know that you will need to use environment variables persist across logins you will need to define them in your bash configuration files. These variables can be found in several places.

  • /etc/environmentThis file creates global, system-wide environment variables which apply to all shells and the GUI. These are in the following format.
    FOO=bar
    VAR_TEST=Test Varable
    
  • /etc/profileThese files are loaded when a bash shell is entered. Use the ExportThese must be declared.
    export JAVA_HOME=/path/to/java/home
    
  • Shell configuration files that can be customized by the user: Each user can create its own configuration files. For bashThe file can be found at ~/.bashrc. Declare variables here in the same way as you would in /etc/profile.
    Export PATH=$HOME/bin :$PATH
    

You don’t need to log out if you have set new environment variables or want to quickly load them into your current shell session. Use the Source command.

jeff@UbuntuVM:~$ source ~/.bashrc
jeff@UbuntuVM:~$ 

Configuring Shell Variables

These variables are not necessarily required to be declared as often as environment variables. SetYou can use the command to list and set all shell variables. You don’t need any arguments to list them all.

jeff@UbuntuVM - $ set

typical output of set command

It will likely be several pages long. You can pipe it through the We offer less command:

jeff@UbuntuVM:~$ set | less

typical output of set command piped through less

You can also use this to print a specific Shell variable echo command.

jeff@UbuntuVM:~$ echo $BASH_VERSION
5.0.17(1)-release
jeff@UbuntuVM:~$ 

It is as easy as typing the variable name into the shell. =symbol, and the value.

jeff@UbuntuVM:~$ MY_VAR="groovy"
jeff@UbuntuVM:~$ 

You can easily manage your Linux Desktop or Server

Setting environment variables and shell variables can be an extremely powerful feature on a Linux computer or server. But be cautious about how you do it. Before you make any changes to files like shell and environment variables, always double-check your typing. Change files like /etc/profileIt is possible to create a backup first.

Knowing which configuration files are being used by users is helpful for user-level changes. Linux has powerful tools that can help you search your hard drives if you are having trouble finding a file.

View Comments (0)

Leave a Reply

Your email address will not be published.