3

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?

KevinLeng
  • 1,843
  • 2
  • 12
  • 7
  • As a matter of idle curiosity, do you know which `bash` version you have on your Solaris machine? I tried on a Solaris 9 box and got `GNU bash, version 2.05.0(1)-release (sparc-sun-solaris2.9)`, which is rather older than I was expecting (but then again, Solaris 9 is not in its first flush of old age, let alone youth). – Jonathan Leffler Mar 07 '13 at 05:05
  • My version is `GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)` – KevinLeng Mar 07 '13 at 05:42

2 Answers2

4

The env and export commands yield the same information, but not in the same format. And bash's export produces a very radically different output from the output of ksh or (Bourne) shell's version. Note that set and export are shell built-in commands, but env 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:

x1=abc
x2=def; export x2
export x3=ghi

There are two exported variables (x2 and x3), and one regular (non-exported) variable. The set command will list all three; export and env 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:

name=value

Classically, the Bourne shell simply listed variables the same way for both set and export.

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 in bash generates assignments with the value protected in quotes. However, the output for export is a declare -x var=value with quote protection. The general idea is presumably that you can use export > file followed by source file to reset the environment variables to the values that were in the environment at the time you did the export.


Summary

  1. Not all shell variables are environment variables.
  2. The set command lists all shell variables and may list functions too.
  3. The export command lists environment variables.
  4. The set and export commands are built into the shell.
  5. The env command with no arguments lists the environment it inherited from the process that executed it.
Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

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 either export or declare -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.

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51