OS: Solaris
Shell: Bash Shell
Scenario: Input the commands separately: "env", "export" and "set" (without any arguments) and there will be a list of variables and values returned.
My question: What's the difference among the returned values after inputting the three commands?
The
set
command shows you all of the shell variables defined in your session.The
export
command lists a subset (usually) of the ones above. These are created with eitherexport
ordeclare -x
: variables which are globally visible - ie., visible to child processes.The
env
command is used to to enable porting scripts from account to another account or machine to machine. env runs a program in a modified or different environment.The
env
andexport
commands yield the same information, but not in the same format. Andbash
'sexport
produces a very radically different output from the output ofksh
or (Bourne) shell's version. Note thatset
andexport
are shell built-in commands, butenv
is an external command that has other uses than just listing the content of the environment (though that is one of its uses).The
set
command lists the variables you've created. This includes environment variables, regular (non-environment) variables, and function definitions (which we'll ignore here).Consider:
There are two exported variables (
x2
andx3
), and one regular (non-exported) variable. Theset
command will list all three;export
andenv
will only list the exported ones.The output of the
env
command is mandated by the POSIX standard. This is simply the variable name and value followed by a newline:Classically, the Bourne shell simply listed variables the same way for both
set
andexport
.Korn shell encloses values in quotes if the value contains spaces or other characters that need protection, but otherwise uses the
name=value
notation.The
set
command inbash
generates assignments with the value protected in quotes. However, the output forexport
is adeclare -x var=value
with quote protection. The general idea is presumably that you can useexport > file
followed bysource file
to reset the environment variables to the values that were in the environment at the time you did theexport
.Summary
set
command lists all shell variables and may list functions too.export
command lists environment variables.set
andexport
commands are built into the shell.env
command with no arguments lists the environment it inherited from the process that executed it.