1

i'm trying to set an environment variable EDITOR and change it from default which is /usr/bin/nano but i can't use set -Ux due to this behavior fish FAQ

How to find out which setting fish inherits from? (so i can change it)

Edit: /etc/environment is empty

set -S EDITOR
$EDITOR: set in global scope, exported, with 1 elements
$EDITOR[1]: |/usr/bin/nano|
$EDITOR: set in universal scope, exported, with 1 elements
$EDITOR[1]: |/usr/bin/nvim|
$EDITOR: originally inherited as |/usr/bin/nano|
6

1 Answer 1

2

In fish, since version 3.6.0, you can ask set --show variable and it will tell you what value fish originally inherited. For example:

> set --show foo
$foo: set in global scope, exported, with 1 elements
$foo[1]: |banana|
$foo: originally inherited as |bar|

This will help you figure out if the value was what fish already got from its parent process at launch, or if it was changed inside of fish.

If your issue is just that things that open an EDITOR open nano, it is entirely possible that nothing actually set an editor and these things default to looking at a list like "nano", "vim", "emacs", "joe" and open the first match. In that case set --show EDITOR would show you nothing.

Or you have it inherited and it's just set in e.g. /etc/bashrc or /etc/profile - distributions often set their default settings in these. Just grep -r EDITOR /etc should show any of these.


If you do have an inherited $EDITOR, and you can't find it in /etc/, there are things you can do, but they are heavily OS-dependent and unportable. Fish isn't the parent process, and you'll need to look at the list of parent processes.

For instance Linux has the /proc/ filesystem where you can find a process' environment in /proc/$pid/environ, with NUL-delimited "var=val" fields.

So you can

  1. get the process' parent pid
  2. look at the parent's environ
  3. go to 1

until you reach a parent pid of 0, which means no parent (this will be init with a pid of 1).

As a quick sketch:

# Start with fish's pid
set -l ppid $fish_pid
# Get the parent pid - ps' output format is fairly terrible and unportable
while set -l ppid (ps -o ppid -p $ppid | string trim)[2]
      and test "$ppid" -gt 0
    # print the pid and the matching environ value
    echo $ppid (sudo grep -z foo /proc/$ppid/environ | string split0)
end

You might get output like:

1305 foo=bar
759
9
8
7
1

This means that the process with pid "1305" had the value "bar" for "foo", and its parents didn't have it in their environ. Note that in this example I set the variable (with export foo=bar) in process 759 - but it didn't have it in its environ since that's the values it has inherited.

So in most cases the process to set it will be the one after the last one with the inherited value.

You can ask ps for information about that process via ps -p 759 (insert your PID as appropriate):

  PID TTY          TIME CMD
  759 pts/0    00:00:00 bash

So in this case a bash process set the variable, and another bash process (1305) inherited it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .