Home » Questions » Computers [ Ask a new question ]

What does 'source' do?

What does 'source' do?

"It exists, and it is runnable. Why isn't there any documentation about it in Ubuntu?
What does it do? How can I install documentation about it?"

Asked by: Guest | Views: 54
Total answers/comments: 5
Guest [Entry]

"source is a bash shell built-in command that executes the content of the file passed as argument, in the current shell. It has a synonym in . (period).


. filename [arguments]

source filename [arguments]"
Guest [Entry]

"It is useful to know the 'type' command:

> type source
source is a shell builtin

whenever something is a shell builtin it is time to do man bash."
Guest [Entry]

"source command executes the provided script (executable permission is not mandatory) in the current shell environment, while ./ executes the provided executable script in a new shell.

source command do have a synonym . filename.

To make it more clear, have a look at the following script, which sets the alias.


#! /bin/bash

alias myproject='cd ~/Documents/Projects/2015/NewProject'

Now we have two choices to execute this script. But with only one option, the desired alias for current shell can be created among these two options.

Option 1: ./make_alias

Make script executable first.

chmod +x make_alias







Whoops! Alias is gone with the new shell.

Let's go with the second option.

Option 2: source make_alias


source make_alias


. make_alias




alias myproject='cd ~/Documents/Projects/2015/NewProject'

Yeah Alias is set."
Guest [Entry]

"When in doubt, the best thing to do is use the info command:

[root@abc ~]# info source

Unless otherwise noted, each builtin command documented in this section
as accepting options preceded by - accepts -- to signify the end of the
options. The :, true, false, and test builtins do not accept options
and do not treat -- specially. The exit, logout, break, continue, let,
and shift builtins accept and process arguments beginning with - with-
out requiring --. Other builtins that accept arguments but are not
specified as accepting options interpret arguments beginning with - as
invalid options and require -- to prevent this interpretation.
: [arguments]
No effect; the command does nothing beyond expanding arguments
and performing any specified redirections. A zero exit code is

. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell
environment and return the exit status of the last command exe-
cuted from filename. If filename does not contain a slash, file
names in PATH are used to find the directory containing file-
name. The file searched for in PATH need not be executable.
When bash is not in posix mode, the current directory is
searched if no file is found in PATH. If the sourcepath option
to the shopt builtin command is turned off, the PATH is not
searched. If any arguments are supplied, they become the posi-
tional parameters when filename is executed. Otherwise the
positional parameters are unchanged. The return status is the
status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or
cannot be read."
Guest [Entry]

"From the Linux Documentation Project, Advanced Bash Scripting Guide,
Chapter 15 - Internals Commands and Builtins:

source, . (dot command):
This command, when invoked from the command-line, executes a script. Within a script, a source file-name loads the file file-name. Sourcing a file (dot-command) imports code into the script, appending to the script (same effect as the #include directive in a C program). The net result is the same as if the ""sourced"" lines of code were physically present in the body of the script. This is useful in situations when multiple scripts use a common data file or function library.
If the sourced file is itself an executable script, then it will run, then return control to the script that called it. A sourced executable script may use a return for this purpose.

So, for those familiar with C programming language, sourcing a file has an effect similar to the #include directive.

Note also that you may pass positional arguments to the file being sourced, like:

$ source $filename $arg1 arg2"