I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:
export PATH=$PATH:/path/to/dir
This works, however each time I exit the terminal and start a new terminal instance, this path is lost, and I need to run the export command again.
How can I do it so this will be set permanently?
Answer
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/environmentList of unique assignments, allows references. Perfect for adding system-wide directories like/usr/local/something/bintoPATHvariable or definingJAVA_HOME. Used by PAM and SystemD./etc/environment.d/*.confList of unique assignments, allows references. Perfect for adding system-wide directories like/usr/local/something/bintoPATHvariable or definingJAVA_HOME. The configuration can be split into multiple files, usually one per each tool (Java, Go, NodeJS). Used by SystemD./etc/xprofileShell 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 forPATHentries 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/profileand/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 script. This is a poor choice because it is single shell specific. Used in non-login mode.. rc
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 includingHOMEorPATHso it has limited use. Used by PAM.~/.xprofileShell 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 extendingPATHwith values such as~/binor~/go/binor defining user specificGOPATHorNPM_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,~/.,_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. Used by shells in login mode._login ~/.. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.rc
Notes
Gnome on Wayland starts user login shell to get the environment. It effectively uses login shell configurations ~/.profile, ~/., ~/. files.
Manuals
- environment
- environment.d
- bash
- dash
Distribution specific documentation
Related
Difference between Login Shell and Non-Login Shell?
No comments:
Post a Comment