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?
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 defaultPATH
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 onFor bash, you can read the details in the INVOCATION section of
man bash
.