73

I am at a bit of a loss as to the purpose of set and export in Bash (and I guess probably other shells too).

I would think it is for setting environment variables, but that can be done just with VARIABLE=VALUE, right?

Also typing set and export on their own show different values.

So what is their purpose?

2

3 Answers 3

86

export marks the given variable as exported to children of the current process, by default they are not exported. For example:

$ foo=bar
$ echo "$foo"
bar
$ bash -c 'echo "$foo"'

$ export foo
$ bash -c 'echo "$foo"'
bar

set, on the other hand, sets shell attributes and the positional parameters.

$ set foo=baz
$ echo "$1"
foo=baz

Note that baz is not assigned to foo, it simply becomes a literal positional parameter. There are many other things set can do (mostly shell options), see help set.

As for printing, export called with no arguments prints all of the variables in the shell's environment. set also prints variables that are not exported. It can also export some other objects (although you should note that this is not portable), see help export.

3
  • 1
    Thanks that is a very useful answer. However, when I set http_proxy just with http_proxy=abc.com, it is visible to child processes, e.g. running wget without me exporting it. How can this be? Commented Apr 4, 2013 at 9:56
  • 4
    @mydoghasworms Two possible reasons. One, you are running the command as foo=bar command. This is an implicit export for a single command. The second possibility is that http_proxy is already exported in your environment before you modify it.
    – Chris Down
    Commented Apr 4, 2013 at 9:58
  • Ah yes, you are right. Brilliant, thanks! Commented Apr 4, 2013 at 10:06
7

See help set or info bash set: set is used to set shell attributes and positional parameters.

Variables that are not exported are not inherited by child processes. export is used to mark a variable for export.

-1

you can think of the 'export' command as turning the variable global...

for better understanding I would append to the previous answers the following example:

x=1
bash -c 'echo $x'
# nothing show

export x
bash -c 'echo $x'
# value of x shown
  • for windows users the use of 'set' is confusing
    because the command is 'accepted' (gives no error)
4
  • this appears to be almost identically the same answer as that posted by Chris Down more than 9 years ago
    – ilkkachu
    Commented Nov 9, 2022 at 17:14
  • is you look closely you will get the nuance of using export in a existing variable... of course this will be more valuable after several commands... or in a script where you want to decide if a previous local variable will be promoted to global... ;-)
    – ZEE
    Commented Nov 9, 2022 at 17:31
  • isn't that exactly what the export foo does in that earlier answer? You're even using the same example command to see if the launched command sees the envvar or not...
    – ilkkachu
    Commented Nov 9, 2022 at 17:32
  • ...and I mentioned previous answers in my answer... but if do not understand the 'nuance' I can't really do much more... I think the noted possibility of decision of 'optional' export of a local variable decided elsewhere in a long script should be enough to be valuable for any programmer... ;-)
    – ZEE
    Commented Nov 9, 2022 at 18:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .