The behavior you're observing is due to the difference between environment variables and shell variables. HISTCONTROL is a shell variable, not an environment variable. Environment variables are a subset of shell variables that are exported to child processes, while shell variables are only available within the current shell session.
Here's a breakdown of why you're seeing this behavior:
-
echo $HISTCONTROL:
- This command directly accesses the shell variable
HISTCONTROL and prints its value. It works because HISTCONTROL is a shell variable.
-
env or printenv:
- These commands list environment variables, not shell variables. Since
HISTCONTROL is not exported as an environment variable, it does not appear in the output of env or printenv.
Verifying with set
To list all shell variables (including environment variables), you can use the set command:
set | grep HISTCONTROL
This will show the value of HISTCONTROL because set lists all shell variables, not just environment variables.
Exporting HISTCONTROL
If you want HISTCONTROL to be an environment variable (so it appears in env or printenv), you need to export it:
export HISTCONTROL=ignorespace:ignoredups
After exporting, HISTCONTROL will appear in the output of env and printenv.
Summary
echo $HISTCONTROL works because it directly accesses the shell variable.
env and printenv do not show HISTCONTROL because it is not an environment variable by default.
- Use
set to list all shell variables, including HISTCONTROL.
- Export
HISTCONTROL if you want it to be an environment variable.