Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
see also: unix.stackexchange.com/questions/3507/… –  lesmana Apr 4 '13 at 10:09

2 Answers 2

up vote 5 down vote accepted

export exports 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, for example, 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.

share|improve this answer
    
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? –  mydoghasworms Apr 4 '13 at 9:56
1  
@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 4 '13 at 9:58
    
Ah yes, you are right. Brilliant, thanks! –  mydoghasworms Apr 4 '13 at 10:06

See help set: set is used to set shell attributes and positional attributes.

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.