Set the environment variable linux

How To – Linux Set Environment Variables Command

  1. Configure look and feel of shell.
  2. Setup terminal settings depending on which terminal you’re using.
  3. Set the search path such as JAVA_HOME, and ORACLE_HOME.
  4. Create environment variables as needed by programs.
  5. Run commands that you want to run whenever you log in or log out.
  6. Set up aliases and/or shell function to automate tasks to save typing and time.
  7. Changing bash prompt.
  8. Setting shell options.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux
Est. reading time 11 minutes

Two types of shell variables

  • Environment variables (GLOBAL): Typically, end-users shouldn’t mess with Environment variables as they are available system-wide, and subshells and child processes can access them. In certain scenarios, we can modify them as per our needs. For example, we set up a system-wide path for the JAVA app or PATH for searching binaries. In almost all cases, we use the export command to define or modify environment variables.
  • Shell and user-defined variables (LOCAL) : As the name suggests, these are defined by users and currently apply to the current shell session.

You can use the following commands to view and configure the environment.

Display current environment variables on Linux

The printenv command shows all or the given environment variables. Open the terminal prompt and then type:
printenv
printenv VAR_NAME
printenv PS1
printenv ORACLE_HOME
printenv JAVA_HOME
# use the grep command/egrep command to filter out variables #
printenv | grep APP_HOME
printenv | egrep ‘APP_HOME|DOCKER_HOME|NIX_BACKUP_HOST’

env command

The env command runs a Linux command with a modified environment. The syntax is:

Please note that If no command name is specified following the environment specifications, the resulting environment is displayed on screen. This is like specifying the printenv command as discussed earlier. For example:

How to set and list environment variables in Linux using set command

The env command/printenv command displays only the Linux shell environment variables. What if you need to see a list of all variables, including shell, environment, user-defined shell functions? Try the following set command for printing environment variables:
set
set | grep BASH
Here is what we see:

The $PATH defined the search path for commands. It is a colon-separated list of directories in which the shell looks for commands. The $PS1 defines your prompt settings. See the list of all commonly used shell variables for more information. You can display the value of a variable using the printf command or echo command:

Outputs from the last command displaying my home directory location set by the $HOME environment variable on Linux:
/home/vivek

Источник

How to Set Environment Variables in Linux

Overview

In this tutorial, you will learn how to set environment variables in 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 unset (clear) existing environment variables.

Environment variables are commonly used within 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, for example, to be stored as environment variables on a server. They are also used to set the important directory locations for many popular packages, such as JAVA_HOME for Java.

Setting an Environment Variable

To set an environment variable the export command is used. We give the variable a name, which is what is used to access it in shell scripts and configurations and then a value to hold whatever data is needed in the variable.

For example, to set the environment variable for the home directory of a manual OpenJDK 11 installation, we would use something similar to the following.

To output the value of the environment variable from the shell, we use the echo command and prepend the variable’s name with a dollar ($) sign.

And so long as the variable has a value it will be echoed out. If no value is set then an empty line will be displayed instead.

Unsetting an Environment Variable

To unset an environment variable, which removes its existence all together, we use the unset command. Simply replace the environment variable with an empty string will not remove it, and in most cases will likely cause problems with scripts or application expecting a valid value.

To following syntax is used to unset an environment variable

For example, to unset the JAVA_HOME environment variable, we would use the following command.

Listing All Set Environment Variables

To list all environment variables, we simply use the set command without any arguments.

An example of the output would look something similar to the following, which has been truncated for brevity.

Persisting 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 sessions ends. This is problematic when we need the variable to persist across sessions.

To make an environment persistent for a user’s environment, we export the variable from the user’s profile script.

  1. Open the current user’s profile into a text editor
  2. Add the export command for every environment variable you want to persist.
  3. Save your changes.

Adding the environment variable to a user’s bash profile alone will not export it automatically. 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 that is used to export an environment variable to allow new child processes to inherit it.

To export a environment variable you run the export command while setting the variable.

We can view a complete list of exported environment variables by running the export command without any arguments.

To view all exported variables in the current shell you use the -p flag with export.

Setting Permanent Global Environment Variables for All Users

A permanent environment variable that persists after a reboot can be created by adding it to the default profile. This profile is loaded by all users on the system, including service accounts.

All global profile settings are stored under /etc/profile. And while this file can be edited directory, it is actually recommended to store global environment variables in a directory named /etc/profile.d, where you will find a list of files that are used to set environment variables for the entire system.

  1. Create a new file under /etc/profile.d to store the global environment variable(s). The name of the should be contextual so others may understand its purpose. For demonstrations, we will create a permanent environment variable for HTTP_PROXY.
  2. Open the default profile into a text editor.
  3. Add new lines to export the environment variables
  • Save your changes and exit the text editor
  • Conclusion

    This tutorial covered how to set and unset environment variables for all Linux distributions, from Debian to Red Hat. You also learned how to set environment variables for a single user, as well as all users.

    Источник

    How to Set Environment Variables in Linux

    Home » SysAdmin » How to Set Environment Variables in Linux

    Every time you start a shell session in Linux, the system goes through configuration files and sets up the environment accordingly. Environment variables play a significant role in this process.

    In this tutorial, you will learn how to set, view, and unset environment variables in Linux.

    What Are Environment Variables: A Definition

    Environment variables are variables that contain values necessary to set up a shell environment. Contrary to shell variables, environment variables persist in the shell’s child processes.

    Structurally, environment and shell variables are the same – both are a key-value pair, separated by an equal sign.

    If a variable has more than one value, separate them with a semicolon:

    Variables that contain spaces are written under quotation marks:

    Note: The convention is to use all caps for writing variable names, in order to distinguish them among other configuration options.

    Most Common Environment Variables

    Here are some environment variables that an average user may encounter:

    • PWD – Current working directory.
    • HOME – The user’s home directory location.
    • SHELL – Current shell (bash, zsh, etc.).
    • LOGNAME – Name of the user.
    • UID – User’s unique identifier.
    • HOSTNAME – Device’s hostname on the network.
    • MAIL – User’s mail directory.
    • EDITOR – The Linux system default text editor.
    • TEMP – Directory location for temporary files.

    How to Check Environment Variables

    View All Environment Variables

    Use the printenv command to view all environment variables. Since there are many variables on the list, use the less command to control the view:

    The output will show the first page of the list and then allow you to go further by pressing Space to see the next page or Enter to display the next line:

    Exit the view by pressing Q .

    Search a Single Environment Variable

    To check a single environment variable value, use the following command:

    The HOME variable value is the home folder path:

    Alternatively, display the value of a variable by using the echo command. The syntax is:

    Search Specific Environment Variables

    To find all the variables containing a certain character string, use the grep command:

    The search output for the USER variable shows the following lines:

    Another command you can use to check environment variables is set . However, this command will also include local variables, as well as shell variables and shell functions.

    Find an environment variable in the list that contains all the variables and shell functions by using set | grep :

    Set an Environment Variable in Linux

    The simplest way to set a variable using the command line is to type its name followed by a value:

    1. As an example, create a variable called EXAMPLE with a text value. If you type the command correctly, the shell does not provide any output.

    2. The set | grep command confirms the creation of the variable. However, printenv does not return any output.

    This is because the variable created in this way is a shell variable.

    3. Another way to confirm this is to type bash and start a child shell session. Using the echo command to search for the EXAMPLE variable now returns no output:

    Note: In a child process, EXAMPLE is not an existing variable.

    How to Export an Environment Variable

    1. If you want to turn a shell variable into an environment variable, return to the parent shell and export it with the export command:

    2. Use printenv to confirm the successful export:

    3. If you open a child shell session now, echo will return the environment variable value:

    The environment variable created in this way disappears after you exit the current shell session.

    Set an Environment Variable in Linux Permanently

    If you wish a variable to persist after you close the shell session, you need to set it as an environmental variable permanently. You can choose between setting it for the current user or all users.

    1. To set permanent environment variables for a single user, edit the .bashrc file:

    2. Write a line for each variable you wish to add using the following syntax:

    3. Save and exit the file. The changes are applied after you restart the shell. If you want to apply the changes during the current session, use the source command:

    4. To set permanent environment variables for all users, create an .sh file in the /etc/profile.d folder:

    5. The syntax to add variables to the file is the same as with .bashrc :

    6. Save and exit the file. The changes are applied at the next logging in.

    How to Unset an Environment Variable

    To unset an environment variable, use the unset command:

    This command permanently removes variables exported through a terminal command.

    Variables stored in configuration files are also removed from the current shell session. However, they are set again upon next logging in.

    To permanently unset a variable you stored in a file, go to the file, and remove the line containing the variable definition.

    After reading this article, you should know how to set and unset environmental variables on a Linux system. Knowing how to set environment variables will help you more easily configure software packages in the future.

    Источник

    Читайте также:  Утилита установки windows 10 64 bit
    Оцените статью