Where Is Path Variable Set in Ubuntu

Where is PATH variable set in Ubuntu?

Grzegorz Żur's answer to another question captures it brilliantly. Unfortunately it was hidden away among many other answers.

There are multiple ways to do it. The actual solution depends on the
purpose.

The variable values are usually stored in either a list of assignments
or a shell script that is run at the start of the system or user
session. In case of the shell script you must use a specific shell
syntax.

System wide


  1. /etc/environment List of unique assignments. Perfect for adding system-wide directories like /usr/local/something/bin to PATH
    variable or defining JAVA_HOME.
  2. /etc/xprofile Shell script executed while starting X Window System session. This is run for every user that logs into X Window
    System. It is a good choice for PATH entries that are valid for
    every user like /usr/local/something/bin. The file is included by
    other script so use POSIX shell syntax not the syntax of your user
    shell.
  3. /etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells.
  4. /etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific.

Also, /etc/environment is not a script file, but rather consists of assignment expressions, one per line. Since this file stores the system-wide locale and path settings, it is most oft quoted choice.
Using /etc/profile is not preferred. It exists only to point to /etc/bash.bashrc and to collect entries from /etc/profile.d

User session


  1. ~/.pam_environment. List of unique assignments. Loaded by PAM at the start of every user session irrelevant if it is an X
    Window System session or shell. You cannot reference other variable
    including HOME or PATH so it has limited use.
  2. ~/.xprofile Shell script. This is executed when the user logs into X Window System system. The variables defined here are visible to
    every X application. Perfect choice for extending PATH with values
    such as ~/bin or ~/go/bin or defining user specific GOPATH or
    NPM_HOME. The file is included by other script so use POSIX shell
    syntax not the syntax of your user shell. Your graphical text editor
    or IDE started by shortcut will see those values.
  3. ~/.profile Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for
    shell-only systems.
  4. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific.

How to permanently set $PATH on Linux/Unix

There are multiple ways to do it. The actual solution depends on the purpose.

The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In case of the shell script you must use a specific shell syntax and export or set commands.

System wide

  1. /etc/environment List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME. Used by PAM and systemd.

  2. /etc/environment.d/*.conf List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME. The configuration can be split into multiple files, usually one per each tool (Java, Go, and Node.js). Used by systemd that by design do not pass those values to user login shells.

  3. /etc/xprofile Shell script executed while starting X Window System session. This is run for every user that logs into X Window System. It is a good choice for PATH entries that are valid for every user like /usr/local/something/bin. The file is included by other script so use POSIX shell syntax not the syntax of your user shell.

  4. /etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells in login mode.

  5. /etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.

User session

  1. ~/.pam_environment. List of unique assignments, no references allowed. Loaded by PAM at the start of every user session irrelevant if it is an X Window System session or shell. You cannot reference other variables including HOME or PATH so it has limited use. Used by PAM.

  2. ~/.xprofile Shell script. This is executed when the user logs into X Window System system. The variables defined here are visible to every X application. Perfect choice for extending PATH with values such as ~/bin or ~/go/bin or defining user specific GOPATH or NPM_HOME. The file is included by other script so use POSIX shell syntax not the syntax of your user shell. Your graphical text editor or IDE started by shortcut will see those values.

  3. ~/.profile, ~/.<shell>_profile, ~/.<shell>_login Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems. Used by shells in login mode.

  4. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.

Notes

GNOME on Wayland starts a user login shell to get the environment. It effectively uses the login shell configurations ~/.profile, ~/.<shell>_profile, ~/.<shell>_login files.

Man pages

  • environment
  • environment.d https://linux.die.net/man/1/environment.d
  • bash
  • dash

Distribution-specific documentation

  • Ubuntu
  • Arch Linux

Difference between Login Shell and Non-Login Shell?

How to set Java environment path in Ubuntu

set environment variables as follows

Edit the system Path file /etc/profile

sudo gedit /etc/profile

Add following lines in end

JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

Then Log out and Log in ubuntu for setting up the paths...

How can I edit the $PATH on linux?

To permanently store your path, you have a few options.

I suggest you read the Ubuntu community wiki on Environment Variables but the short answer is the best place is ~/.profile for your per-user PATH setting or /etc/profile for global settings.

Change PATH:

  1. Append something to your PATH

    export PATH=$PATH:/your/new/path/here
  2. Override your PATH (save backup before!)

    export PATH=:/your/new/path/here:/another/new/path/here

How to set an environment variable to point to a location and how to set path of an environment variable in UBUNTU?

Yes, the above command sets variable when executing together with other commands like so:

$ VAR1=/home/folder1/lib123.so MY_AWESOME_COMMAND

Or you can use export so that you won't have to include the variable in each command.

$ export VAR1=/home/folder1/lib123.so

Test it below:

$ echo $VAR1
$ /home/folder1/lib123.so

PATH environment variable in Linux

I would like to add a few more details to what @cnicutar has already mentioned.

Environment variables including PATH can be:

  • System wide - The values of the environment variables last till the system is up
  • Session wide - Lasts till a session lasts (till user logs out)

/etc/profile is meant for system settings for Bourne & Bourne compatible shells. The behavior of /etc/profile may vary across distributions.

For the latest Ubuntu distributions, it is recommended to use /etc/environment for system-wide settings. It is not recommended to use /etc/profile or /etc/bash.bashrc as noted the Ubuntu help.

On Ubuntu machines, /etc/profile is a shell script which sources the scripts in /etc/profile.d and the system-wide bashrc file in /etc/bash.bashrc, whereas /etc/environment is a text file consisting of variable assignments per line which are set into the system-wide environment.

For each user the values of environment variables including PATH (for the shell) can also be manipulated through ~/.profile, ~/.bash_profile, ~./bash_login, and ~/.bashrc where ~ means the user's home directory, like /home/alex/.

To see your current environment variables and their values, you can use printenv.

You can refer to the following link for more details on environment variables on Ubuntu systems: https://help.ubuntu.com/community/EnvironmentVariables



Related Topics



Leave a reply



Submit