2

Exercise 2 from Missing Semester asks the following:

Run PATH= and try running the previous commands again, some work and some don’t, can you figure out why?

Setting PATH= removes all the contents from the PATH variable. Then echo $PATH yields just an empty line. But when I restart the shell, all the contents of the PATH variable are there again. My question is where does PATH variable get its values from when I restart the shell?

1 Answer 1

6

In general, every process inherits its environment (including a value for the PATH) from its parent. So if you start a shell from a terminal emulator, the shell inherits from the terminal which inherits from the desktop session and so on. If you follow the process tree back, you will find that a default PATH value is read from the /etc/environment file by the pam_env module when a user's session is initiated.

Additionally, if you are using Windows Subsystem for Linux (WSL), path components may be inherited from the Windows host session via the appendWindowsPath interoperability feature.

For the specific case of shells, the PATH variable may additionally be set or modified by various system-wide and/or user-specific configuration files, depending on

  • which shell you are using (bash, dash, ksh, zsh, csh etc.)
  • how the shell is invoked (as a login shell, as an interactive non-login shell, or as a non-interactive shell)

For bash, you can read the details in the INVOCATION section of man bash.

2
  • Oh, I see! Most of the values are listed in /etc/environment. I am using Ubuntu on WSL2, there are some additional values in PATH that start with /mnt, which are not listed there. Any idea where those come from?
    – Terrarium
    Commented Apr 30, 2022 at 13:22
  • @Terrarium thanks for the reminder - I have added a note about that Commented Apr 30, 2022 at 13:32

You must log in to answer this question.

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