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?

  • see also: http://unix.stackexchange.com/questions/3507/difference-between-environment-variables-and-exported-environment-variables-in-b – Lesmana Apr 04 '13 at 10:09
  • I always start by bash script with the following two. #!/usr/bin/env bash set -euxo pipefail This link beautifully explains the same. https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-euxo-pipefail – Sankarganesh Eswaran Jun 21 '18 at 07:23

3 Answers3

85

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.

ilkkachu
  • 138,973
Chris Down
  • 125,559
  • 25
  • 270
  • 266
  • 1
    Thanks that is a very useful answer. However, when I set http_proxy just with http_proxy=http://abc.com, it is visible to child processes, e.g. running wget without me exporting it. How can this be? – mydoghasworms Apr 04 '13 at 09: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 Apr 04 '13 at 09:58
  • Ah yes, you are right. Brilliant, thanks! – mydoghasworms Apr 04 '13 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.

choroba
  • 47,233
-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)
ZEE
  • 257
  • this appears to be almost identically the same answer as that posted by Chris Down more than 9 years ago – ilkkachu Nov 09 '22 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 Nov 09 '22 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 Nov 09 '22 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 Nov 09 '22 at 18:00