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 can I permanently export a variable in Linux?
You can add it to your shell configuration file, e.g., $HOME/.bashrc or more globally in /etc/environment.
After adding these lines, the changes won't reflect instantly in GUI-based systems. You have to exit the terminal or create a new one and on the server, log out the session and log in to reflect these changes.
How do I permanently add an environment variable?
Edit your ~/.bashrc file and place at the end:
export JAVA_HOME=jdk-install-dir
export PATH=$JAVA_HOME/bin:$PATH
You can even modify /etc/environment
if you want it to persist for all users.
In Ubuntu WSL, how can you store permanent environment variables?
So this is the way that worked for me:
~/.bash_profile
is the correct file for permanent environment variables if using Bash on Ubuntu WSL2, however make sure you add the export
keyword before your variables, like so:
export THISVAR=thisIsAVar
export ANOTHERVAR=anotherVar
To add the variable(s) use the command sudo nano ~/.bash_profile
(if you prefer Nano editor) or sudo vim ~/.bash_profile
(if you prefer Vim). After you have entered your variables into the .bash_profile
save the file and then enter source ~/.bash_profile
to have the variables available in the terminal.
How to permanently set env across every session in fish shell?
Doesnot global means every session?
It doesn't. It's "global" as opposed to "local". From the fish documentation:
Global variables are specific to the current fish session, and will never be erased unless explicitly requested by using set -e.
In general, what you want is to just put the set -gx
into ~/.config/fish/config.fish. That's fish's configuration file.
Fish also has "universal" variables, which are stored persistently, but they interact awkwardly with exporting so I wouldn't recommend it.
For $PATH specifically, fish offers the fish_user_paths
variable that it adds automatically, so you can run
set -U fish_user_paths $fish_user_paths $HOME/.cargo/bin
once, interactively, and fish will take care of it. This is a universal variable, but fish takes care to add it to $PATH when necessary (for each component it checks if it's already there and such). Do not put this into config.fish, or it will add one $HOME/.cargo/bin every time you start a fish, and so it would balloon the variable.
To recap:
- For global variables, put the
set
statement into config.fish - For universal variables, execute it manually
- For $PATH, use $fish_user_paths for your customizations
Permanently set environment variables using Ansible
Thank you.
I wrote this ansible-playbook and it's working.
- hosts: localhost
become_user: root
tasks:
- name: Adding KUBECONFIG variable
delegate_to: localhost
copy:
content: export KUBECONFIG="/etc/config/admin.conf/admin.conf"
dest: /etc/profile.d/kubeconfig.sh
Related Topics
Hbase Does Not Run After ./Start-Hbase.Sh - Permission Denied
How to Change File Extension in Linux Shell Script
Changing File Permissions Linux
Vagrant - How to Mount The Virtualbox Shared Folder? ("Vboxsf" Not Available)
How to Compare 3 Files Together (To See What Is in Common Between Them)
Trying to Delete All But Most Recent 2 Files in Sub Directories
Replace in a CSV File Value of a Column
How to Simulate a Iret on Linux X86_64
Binding on a Port with Netpipes/Netcat
Linux Awk Comparing Two CSV Files and Creating a New File with a Flag
Can Inotify Tell Me Where a Monitored File Is Moved
Docker Rootless Unable to Pull Images
How to Select a Static Port Number for a Custom App
When Did Hup Stop Getting Sent and What How to Do About It