Environment Variables – How to Use `env` to Print Environment in Current Shell

environment-variables

coreutils manual says about env command that

env [option]... [name=value]... [command [args]...]

If no command name is specified following the environment specifications, the resulting
environment is printed. This is like specifying the printenv program.

is the printed environment by env specific to env, in the sense that the value of _ depends on env? For example,

$ env | grep '^_'
_=/usr/bin/env

Does env print out almost all the the exported environment of the current shell, except that the value of the environment variable _ is modified to be specific to env?

So is env not to print out the environment in the current shell, but the environment received within env?

Thanks.

Best Answer

env is a separate program in most shells, so it prints the environment it received from the shell.

The Bourne Shell uses an environment management system based on shell variables.

At startup, it imports the environment into the list of shell variables.

Creating or modifying shell variables does not modify the environment that the shell sets up for new programs.

  • calling set prints the internal shell variables as set is built into the shell

  • calling env prints the exported environment as env is an external command

The shell variable _ was introduced by ksh88. It holds the last command.