How to correctly set the PATH variable

A lot of tools (like rustup) modify the PATH variable by adding some code to the ~/.profile file.
Usually the .profile file is sourced when logging in.

In clear linux ~/.profile is not sourced.

What’s the recommended way to edit the PATH variable?

1 Like

It should read ~/.profile as far as I know. It should also read /etc/profile and /etc/profile.d/*
See this threads comments: Alias as root not working

It does read /etc/profile and /etc/profile.d because in the default ~/.bashrc there is

if [ -f /usr/share/defaults/etc/profile ]; then
	. /usr/share/defaults/etc/profile
fi
# allow admin overrides
if [ -f /etc/profile ]; then
	. /etc/profile
fi

But it doesn’t source ~/.profile anywhere. In other distros the file ~/.profile is sourced when logging in.
I know the file is not sourced when running gnome on wayland but in clear linux it’s not sourced even when running gnome on xorg.

According to the bash man page, bash is supposed to read it automatically, but I think it has depended on /etc/profile to do this - so, from a quick glance, this looks like a bug based on an incorrect statement in the bash man page. Worth fixing, of course.

It did source ~/.profile for me with an interactive SSH login, but not in a desktop environment as @ranfdev is suggesting.

The way I’m understanding the documentation, it sounds like this is the designed behavior since launching GNOME terminal is not a login-shell?: Bash Startup Files (Bash Reference Manual)

Yes, It is the designed behavior of bash. But usually, desktop environments are launched from a login shell, so every terminal you open inside that session inherits all of the environment variables (even if the terminal is not launching a login-shell).
The gnome team has written about this, but here the situation is a bit different:
~/.profile is not sourced even on Gnome on xorg
I can’t get the workaround using environment.d to work (the path variable is still the same, even after adding a file with the correct config under ~/.config/environment.d/path.conf

At the moment i’ve set gnome terminal to launch a login-shell every time, so i can access my environment variables, but only from gnome terminal

Stumble upon this thread when i was searching for the problem. So i decided to find a workaround for it (especially since i dealt with VSCode not recognizing PATH.
One workaround that you mentioned is using the login-shell inside the terminal profile. However, this solution won’t work for other application with built in terminal (VSCode my case)

My solution to this was to update ~/.bashrc
with:

if [ $TILIX_ID ] || [ $VTE_VERSION ]; then
        source /etc/profile.d/vte.sh
fi

(just in case you don’t trust a random guy’s comment, here is where i got it from :Tilix: VTE Configuration)
Now, since we provided vte.sh location. terminal will complain, because there is no such file and/or directory.
however, i created the folder profile.d and added this file in it because i am lazy:
vte/vte.sh at ccf2f926f3c0d1ef545191a0e2edb9b1181aa60f · jessevdk/vte · GitHub
Now you have a working PATH.
to add to path, i edit .profile and add to it. (for some reason, terminal command export doesn’t always work)
Warning: Do not create the profile.d directory without adding the file in. Leaving an empty profile.d might cause the system to crash at restart. Be sure to have vte.sh file in it

After adding those, Rust Cargo runs VSCodium PATH works too, in addition to terminal inside VSCodium

3 Likes

btw you should really highlight the Warning :slight_smile:

Be careful if you set your variables on /etc/profile. If you are extending PATH variable, you should do it like the example below, otherwise it will be overwritten and your distro won’t work properly on boot.

# /etc/profile
PATH="$PATH:/usr/lib/jvm/jdk-11.0.6/bin" 

DON’T FORGET to put $PATH: at the beginning.

Thanks, Vitality,

It worked perfectly for me!