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 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
Environment variable PATH on linux
I would setup a new alias in my .bashrc
or .profile
, which should be located under your home directory. Add the following to the end of the file:
alias firefox="/home/debian/firefox/firefox"
Save the file and reload it using:
source ~/.bashrc
Since you added the alias to your .bashrc
this alias will be created everytime you open a new instance of a shell.
You could use nohup
to keep the command running after the shell session ends:
alias firefox="nohup /home/debian/firefox/firefox &"
Notice the trailing &
character, which will run the command in the background so you can keep using your terminal.
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
Error in setting the path variable in Linux
If you want to set the path environment variable, you need to use:export PATH=/some/path:$PATH
.
In your case, it would look like:export PATH=$HOME/Toolchains/gcc-arm-none-eabi-9-2020-q2-update/bin:$PATH
The only downside to setting the path manually is that it will not persist across bash sessions. If you want this change to be permanent, then I would suggest adding that line to your .bashrc
or .profile
file in the home directory.
Where default PATH environment variable is defined
Where default PATH environment variable is defined
It depends. On the distribution, on the shell, on the environment, on OS, on configuration files.
I have some systems at hand, I see:
- ArchLinux adds to PATH in /etc/profile.
- OpenSuSE has yet a different code that adds to PATH from /etc/profile
- CentOS7 has has yet a different code in /etc/profile
I suspect that each distribution places files in slightly different places, they will ship with slightly different configuration.
how the environment is populated?
Bash has DEFAULT_PATH_VALUE that is used when PATH is not set. But don't look at the default value too much - package distrbutors overwrite it.
There is /etc/environment
(and pam_env.conf
) that are read by PAM on login. So when you login. But not when you chroot
.
The is /etc/profile
and ~/.profile
files read by the shell. These scripts may read other files. So usually there is drop-in dir /etc/profile.d/*
.
In bash there is /etc/bashrc
or /etc/bash.bashrc
/etc/bash.login
/etc/bash.logout
or similar and user configuration files ``~/.bashrcetc. In bash also
/etc/bash_completion.dand all scripts in
/usr/share/bash_completionare also sourced, when completion is enabled. BUT these files are for Bourne shell - there are also other shells, notably
zshand
csh`, and people use them and they have different syntax and different set of startup files.
Each of these scripts can manipulate PATH
, it can reset it, append to it, do whatever it wants to it.
And there are also different set of files read on login/non-login + interactive/non-interactive shells, and these files can include each other (it's common to have .bash_profile
or other files that just source .bashrc
or similar).
What are defaults
The /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
looks like a good default that should be safe on any linux.
by the way, is surprisingly empty. Why?
My /etc/environment
file is just empty and has only the default comment in linux-pam
package. /etc/environment
has a very simple syntax and is read by pam
(there may be no pam on your system...). To modify shell environment, one would prefer shell files - so /etc/profile
is the place to keep system modifications. Also /etc/environment
has simple key=value syntax, wherea's in /etc/profile
you have, well, shell. And /etc/environment
is read on login by PAM - so putting PATH
just there would break I think for example docker
, it just runs a shell in chroot, there is login.
Related Topics
Warning: The Use of 'Tmpnam' Is Dangerous, Better Use 'Mkstemp'
Conversion from Ebcdic to Utf8 in Linux
How to Make Library Installed from Opam Available to Ocaml
How to Generate Files in a Docker Container for Having The Same Owner as The Host's User
Elk Not Passing Metadata from Filebeat into Logstash
How to Do "For Each" on Output from Find
Monodevelop - Runs Only Using Sudo
How to Combine Two Search Words with "Grep" (And)
Find String and Replace Line in Linux
Capturing User-Space Assembly with Ftrace and Kprobes (By Using Virtual Address Translation)
Running Linux Container on Docker Windows
Copy File to Backup Directory Preserving Folder Structure
Gnuplot-Like Program for Timeline Data