How to Permanently Set $Path on Linux/Unix

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 Permanently update PATH variable in Unix

If this is for all users, place this in /etc/profile or /etc/bashrc
If just for a given user, place it in his/her ~/.bashrc.

I hope this helps.

change PATH permanently on Ubuntu

Add

export PATH=$PATH:/home/me/play

to your ~/.profile and execute

source ~/.profile 

in order to immediately reflect changes to your current terminal instance.

How to permanently set $PATH on Raspbian GNU/Linux 10

How to permanently set $PATH on Raspbian GNU/Linux 10

To explicitly permanently change PATH for all possible environments that do not have PATH is explicitly set, like a new non-interactive non-login shell that does not inherit PATH from the parent process, recompile bash with different value of DEFAULT_PATH_VALUE (there's a ./configure option for it if I remember correctly).

Where do I specify the path so that the samba binary could be always found?

You specify it in your script.

PATH=$PATH:/some/path
# or explicitly
bindir=$(/the/path/to/samba -b ....)

You could also explicitly invoke a login shell when running the script, ergo sourcing /etc/profile* stuff.

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.

Setting PATH environment variable in OSX permanently

You have to add it to /etc/paths.

Reference (which works for me) : Here

How do I set Path Variables in Linux just like in windows?

You could set your path variable but you really shouldn't be doing it like that.

Since you don't specify which version of Linux I'll be explaining it for Ubuntu and Arch Linux.

You should install openjdk packages with your package manager like so:

Ubuntu:

sudo apt-get install openjdk-8-jdk

Arch linux:

sudo pacman -S jdk8-openjdk

You can then use the following commands to change between those versions:

Ubuntu

sudo update-alternatives --config java

Arch Linux

archlinux-java set java-8-openjdk

You can even launch specific apps in specific versions but this would be too much to cover for now.




Setting the path variable regardless

To change your $PATH you have to either edit ~/.profile (or ~/.bash_profile) for each user or global $PATH setting in /etc/profile.

Simply append another path to it like so:

/usr/local/bin:/usr/local/sbin:/custom/path

You can also do:

PATH = $PATH:/custom/path


Related Topics



Leave a reply



Submit