source (command)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Widefox (talk | contribs) at 00:20, 4 September 2018 (Added {{merge to}} tag to article (TW)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

source is a Unix command that evaluates the file following the command, as a list of commands, executed in the current context.[1] Frequently the "current context" is a terminal window into which the user is typing commands during an interactive session.

The source command can be abbreviated as just a dot (.) in Bash and similar POSIX-ish shells. However, this is not acceptable in C shell, where the command first appeared.

Some Bash scripts should be run using the source your-script syntax rather than run as an executable command, e.g., if they contain a change directory (cd) command and the user intends that they be left in that directory after the script is complete, or they contain an export command and the user wants to modify the environment of the current shell. Another usage situation is when a script file does not have the "execute" permission. Passing the script filename to the desired shell will run the script in a subshell, not the current context.

References

  1. ^ "BASH BUILTIN COMMANDS", the (GNU) Linux man page for "source" in Bash-3.0. 2004 Apr 20