What is the difference between source and export?

linux, unix

Solution

When you `source` the file, the assignments will be set but the variables are not exported unless the `allexport` option has been set. If you want all the variables to be exported, it is much simpler to use `allexport` and `source` the file than it is to read the file and use `export` explicitly. In other words, you should do:

set -a
. file.txt

(I prefer `.` because it is more portable than `source`, but `source` works just fine in `bash`.)

Note that exporting a variable does not make it an environment variable. It just makes it an environment variable in any subshell.

Problem

I am writing a shell script, to read a file which has key=value pair and set those variables as environment variables. But I have a doubt, if I do `source file.txt` will that set the variables defined in that file as environment variable or I should read the file line by line and set it using export command ? Is source command in this case different than export?

Original source

Related problems