$EDITOR and $BROWSER environment variables being reset after loading ~/.profile

Hello, recently I noticed that, although I have the EDITOR and BROWSER environment variables set in ~/.profile, which bash reads upon logging into the system, their values when I inspect them in an interactive bash session are not what I set them to.
Instead they are set to their respective default values of /usr/bin/nano and /usr/bin/firefox.

By searching for people encountering similar problems on this forum and elsewhere, everyone who had run into something somewhat similar were able to solve their issues by setting x or y variable in .bashrc, .bash_profile, .profile, or other such configuration files.

What’s more, I know that ~/.profile is read, since other variables that are set there do have the appropriate values when I inspect them in an interactive session.

Is there a file that sets these variables after these configuration files are loaded? Does KDE Plasma sets these variables after login, which overwrites what is in .profile? If so, where does it do that?

Here are the contents of my .profile and .bash_profile files. You can see that .bash_profile sources .profile if it exists:

#
# ~/.profile
#

export XDG_CONFIG_HOME="$HOME/.config"
export STARSHIP_CONFIG="$XDG_CONFIG_HOME/starship.toml"
export DOOMDIR="$XDG_CONFIG_HOME/doom"
export EDITOR="$HOME/Appimages/nvim.appimage"
export BROWSER="/usr/bin/vivaldi-stable"
#
# ~/.bash_profile
#

[[ -f ~/.bashrc ]] && . ~/.bashrc

if [ -r ~/.profile ]; then . ~/.profile; fi
case "$-" in *i*) if [ -r ~/.bashrc ]; then . ~/.bashrc; fi;; esac

Edit1: To clarify, I do export these variables in .profile in addition to setting them.

Edit2: I discovered something interesting. I tried exporting the VISUAL variable to the same value as that of EDITOR by adding this line in .profile:
export VISUAL=$EDITOR
When I echo $VISUAL after logging out then logging bask in, I see that VISUAL is correctly set to the path I wanted (namely $HOME/Appimages/nvim.appimage), but EDITOR is back to being the default /usr/bin/nano. This means that .profile is correctly read and all of the variables it exports are correctly exported, and that EDITOR was at some point the value I set it to, but something somewhere is resetting EDITOR and BROWSER back to their default values. (The same goes for BROWSER btw.)

check you shell’s rc files .bashrc or .zshrc or ?

anyway - a script of any kind can reset those variables to fit it’s own purpose.

bash does not read ~/.profile if ~/.bash_profile exists, and in Manjaro, that file does indeed exist.

In addition to the above, setting the variables isn’t sufficient. You need to also export them, or else they will only be used within the current shell, and not in any subshells ─ which includes scripts.

2 Likes

Are you sure?

Because on my system .profile is indeed sourced when a shell is opened.

My .profile looks like this

# set PATH so it includes user's private bin if it exists
export QT_QPA_PLATFORMTHEME="qt5ct"
export GTK2_RC_FILES="$HOME/.gtkrc-2.0"

export EDITOR=/usr/bin/micro
export VISUAL=/usr/bin/xed

EDIT: The above is not correct - I forgot only to later realize I had the exports in both .profile and .xprofile - which lead to incorrect conclusion.

1 Like

Hmm… Now I’m not sure about that anymore. :slightly_frowning_face: Apparently I too have a ~/.profile, albeit that it contains only a single line.

export EDITOR=/usr/bin/nano

:man_shrugging:

This is the content of my .bash_profile file. From the little bash that I know, I believe that whenever it is read, it will also source .profile, or was I mistaken? Also, I am in fact exporting these variables in my .profile file.

#
# ~/.bash_profile
#

[[ -f ~/.bashrc ]] && . ~/.bashrc

if [ -r ~/.profile ]; then . ~/.profile; fi
case "$-" in *i*) if [ -r ~/.bashrc ]; then . ~/.bashrc; fi;; esac

Edit: Here are the contents of my .profile file, just in case.

#
# ~/.profile
#

export XDG_CONFIG_HOME="$HOME/.config"
export STARSHIP_CONFIG="$XDG_CONFIG_HOME/starship.toml"
export DOOMDIR="$XDG_CONFIG_HOME/doom"
export EDITOR="$HOME/Appimages/nvim.appimage"
export BROWSER="/usr/bin/vivaldi-stable"

Also, again I am sure that .profile is actually read, since the other variables that I’m exporting there are visible and set to their correct values when I look at them in a terminal after logging in.

I have indeed checked those files, and I cannot find any place where they source a script that changes these variables. The only script my .bashrc file sources are the one that configures the shell’s completion feature, and a .alias file that I created in which only aliases are define, and nothing else.
I don’t use zsh but just to be sure I checked zshrc, and it only sources two files if they exist, namely /usr/share/zsh/manjaro-zsh-config and /usr/share/zsh/manjaro-zsh-prompt. The latter just configures a prompt for zsh, and the former does have a line that exports EDITOR as the path that I see, but that line is in fact commented out.

By doing some simple experimenting, I have discovered that .profile is correctly read and its variables correctly exported, but that EDITOR and BROWSER are set back to their default values afterward in some other script that is called later. Where exactly they are reset tough, I do not know. Nothing in the .bashrc, .bash_profile and .profile in my home directory are doing such a thing. I am suspecting that some Manjaro configuration script that is called during login or something is causing it, but I don’t know exactly.
I’ve edited the original post to add this information.

It is possible that those variables get overridden by something in /etc/profile or /etc/environment. It shouldn’t happen, but it’s possible.

Just checked those. /etc/environment is empty, and /etc/profile sources /etc/bash.bashrc and scripts in /etc/profile.d, but none of them seem to overwrite these values.

There is this thing with configurations - they are checked and applied in order of precedence.

  1. Installation configs - usually in the /usr tree
  2. Local system configs - usually in the /etc tree
  3. User configs - found in your home folder.

Then any script you launch wheter this is a local script or script in system path can alter those values to suit application specific configurations not a part of an ordinary configuration - e.g. a library path which is not needed system wide but the application needs it.

So if you need to set the e.g. $BROWSER variable - it can be set and exported in ~/.profile which is used in TTY or in terminal emulators.

~/.xprofile and the rc files in your home are places where settings for the environment can be controlled.

E.g. for my system to apply Kvantum themes - it is not enough to export the QT_QPA_PLATFORMTHEME in .profile - it must be in ~/.xprofile for the export to have any effect on the qt environment.

2 Likes

@linux-aarhus’ post is most likely to become a solution, I just want to suggest checking if you have ~/.pam_environment and what’s written there while you are on your way of investigating your dotfiles.

Sadly, even exporting these variables in .xprofile did nothing to solve the issue. And to @openminded, ~/.pam_environment only contained two lines about fcitx, so nothing so see there.

Given how persistent and hard to track this issue seems to be, I’m considering throwing the towel for the time being and exporting these variables in .bashrc so they will be correctly set as soon as I open a terminal, although that is not ideal. It’s not a really big deal, but it’s still kind of a nuisance because every time I make a git commit and forget that EDITOR is set to nano (which I uninstalled since I don’t use it), I get an error and have to set it back to vim. And as for BROWSER, I would like to use it to specify a default web browser so that it may be provide a very simple way to make, say, a keyboard shortcut to open my current web browser, and since I might use such a shortcut before opening any terminal, setting that variable in .bashrc is not ideal, but oh well.

I guess I’ll keep this discussion open, in case I miraculously figure out a solution that I may share, or someone else has an idea that turns out to work. In the meantime, thank you for your help.

You can always check your variables with the env command - maybe you already know that.

There is another way of testing

  1. create one nonsense export in .profile e.g. export FOO=foo
  2. create a second nonsense export in .xprofile e.g. export XFOO=xfoo

Log out and login - open a terminal emulator

echo $FOO
echo $XFOO

Switch to a TTY - login and repeat

echo $FOO
echo $XFOO

The result of the above may not give you the same result as I get - e.g. if you source your .profile in your shell running in x you may get the content of both variables.

Other environments than mine - based on Openbox and Tint2 - may have their own logic which may include sourcing .profile - I have no way of knowning this.

This is new info and if the main issue with EDITOR is git - there is another way

There is two ways

git config --global core.editor "nvim"

Or export the env variable GIT_EDITOR like

export GIT_EDITOR=nvim

The BROWSER variable is nonstandard and therefore a hit and miss depending on the system and the app. One of the places it is useful is with the Python urllib but for the x-system the default browser is specified in ~/.config/mimeapps.list. Of course you can make use for it yourself but you would have to explicitly reference the variable - you cannot depend on the system knowing that a html document should be opened by the $BROWSER.

While there are utilities connected to mimeapps - I have personally found them hard to use - so I lookup the desktop launcher and edit the file manually. Example searching for firefox - in your case you would use the string vivaldi

grep -rl 'firefox' /usr/share/application 
[Default Applications]
...
text/html=firefox.desktop;

[Added Associations]
...
text/html=firefox.desktop;

The PoC way is to produce the above result is create an empty html document (not completely empty include the basic tags <html><head></head><body></body></html> ) .

Use your filemanager to

  • rightclick on the file
  • select open with
  • open with
  • point to the browser you prefer
  • check the box Set selected app as default …
  • click OK

To emphasize what to expect when exporting variables

.profile is used by TTY and can be sourced by e.g. .bashrc
.xprofile is used by the x-server

To have a variable exported and available in a terminal emulator - which I dare to say - probably is the majority of the members of this forum - you must use .xprofile.

Triggered by this topic I ran some experiments and I learned

  • I am correcting myself from one of my comments - @aragorn - you are right - I am wrong (I had the exports in both)
  • what is defined in .profile is not read by the x-server
  • to export variables in the x-org environment you use .xprofile
3 Likes

I did not know that BROWSER was non-standard, until now I was using the mime utilities that you mentioned but also found them hard to use, hence why I hoped to be able to use that variable instead.
Thank you for the info on how to define the git editor from within git directly! I should have thought about it in hindsight.
I’ll be careful where I export my variables in the future.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.