bashenvironment-variables

Some environment variables still set in bash even when everything is purportedly unset


I tried to remove all environment variables for the current shell and for the subshell ('bash-3.2'):

unset $(env | cut -d= -f1) OLDPWD
export

For the shell, everything turned out as intended, but there were some problems with a subshell:

/bin/bash
export

Output:

declare -x OLDPWD
declare -x PWD="/Users/alex"
declare -x SHLVL="1"
declare -x _="/bin/bash"

And:

env

Output:

PWD=/Users/alex
SHLVL=1
_=/usr/bin/env
  1. Bash has created variables. There is no file in the home directory .bashrc and other files from which bash can take data at startup. Where did he get these variables from?
  2. How can bash execute non-builtin commands if PATH is not set?

Solution

  • Bash has created variables. ... Where did he get these variables from?

    From Bash. Like literally, from Bash source code.

    https://github.com/bminor/bash/blob/f3a35a2d601a55f337f8ca02a541f8c033682247/variables.c#L858

    How can bash execute non-builtin commands if PATH is not set?

    When PATH is not set, Bash uses some "default PATH". There is a macro in the source code, and it can be changed with the compile ./configuration option.

    https://github.com/bminor/bash/blob/f3a35a2d601a55f337f8ca02a541f8c033682247/config-top.h#L66 and https://github.com/bminor/bash/blob/f3a35a2d601a55f337f8ca02a541f8c033682247/variables.c#L495


    Use env -i to run something in a blank environment.