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/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.
/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.
/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.
/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.
/etc/<shell>.<shell>rc
. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.
User session~/.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.
~/.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.
~/.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.
~/.<shell>rc
. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.
Notes
/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.
/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.
/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.
/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.
/etc/<shell>.<shell>rc
. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.
~/.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 includingHOME
orPATH
so it has limited use. Used by PAM.~/.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 extendingPATH
with values such as~/bin
or~/go/bin
or defining user specificGOPATH
orNPM_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.~/.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.~/.<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
Related
- Ubuntu
- Arch Linux
Related
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
/etc/environment
List of unique assignments. Perfect for adding system-wide directories like/usr/local/something/bin
toPATH
variable or definingJAVA_HOME
./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 forPATH
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./etc/profile
and/etc/profile.d/*
Shell script. This is a good choice for shell-only systems. Those files are read only by shells./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
~/.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
includingHOME
orPATH
so it has limited use.~/.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 extendingPATH
with values
such as~/bin
or~/go/bin
or defining user specificGOPATH
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.~/.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.~/.<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
How to Reload Google Chrome Tab from Terminal
Argument List Too Long Error For Rm, Cp, Mv Commands
Using Openssl to Get the Certificate from a Server
How to Recursively Grep All Directories and Subdirectories
How to Know the Script File Name in a Bash Script
Linux: Copy and Create Destination Dir If It Does Not Exist
Controlling a Usb Power Supply (On/Off) With Linux
Edit Shell Script While It's Running
Assembly Segmentation Fault After Making a System Call, At the End of My Code
Kernel Stack and User Space Stack
How to Simulate Just One Enter in Command Line After Executing a Jar File
How to Write a Bash Script to Restart a Process If It Dies
How to Make a Program Continue to Run After Log Out from Ssh
Peak Memory Usage of a Linux/Unix Process
Tool to Trace Local Function Calls in Linux
How to Copy Commits from One Git Repo to Another
How to Show All Shared Libraries Used by Executables in Linux