789
$ whatis source
source: nothing appropriate.
$ man source
No manual entry for source
$ source
bash: source: filename argument required
source: usage: source filename [arguments]

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?

7
  • 9
    related: superuser.com/questions/176783/…
    – Lesmana
    Jan 17, 2011 at 21:59
  • 83
    you forgot $ type source source is a shell built-in
    – bnjmn
    Oct 9, 2013 at 6:00
  • 4
    My shell returned this $ whatis source source (1) - bash built-in commands, see bash(1). Also, man source takes me to the BASH_BUILTINS(1) man pages. This is on Fedora btw, no idea why those debian packages are un-(or badly)-documented.
    – arielnmz
    Aug 20, 2014 at 12:29
  • 6
    @lesmana, great link. That linked answer is the more thorough answer to this question.
    – Scott
    Sep 5, 2014 at 15:02
  • 12
    Try "help source"
    – Jasser
    Sep 19, 2015 at 14:12

12 Answers 12

624

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

Syntax

. filename [arguments]

source filename [arguments]
9
  • 15
    Is source a bash specific command or do other shells have it too? (I'm asking to get tags right on the question...)
    – Jonik
    Sep 24, 2009 at 11:17
  • 19
    @nagul, source was not present in the Bourne shell, it is a GNU extension that came much later. The original and still portable syntax (POSIX) is to use the "dot" command, i.e. . instead. I personnaly never use source given the fact it is longer to type and has no added value. I guess its main purpose is to make scripts more readable for newbies.
    – jlliagre
    Aug 2, 2013 at 14:02
  • 39
    @jlliagre my personal "explain why have source" is that source is not only more descriptive, but it looks like something other than a typo. I've had people skip the period/dot when I send tech commands in email. Mar 10, 2014 at 18:31
  • 4
    One common use for this command is for a shell script to source in a "configuration file" that contains mostly variable assignments. The variable assignments then control things the rest of the script does. Of course, a good script will set variables to sensible defaults before the source, or at least check for valid values.
    – LawrenceC
    Mar 10, 2014 at 18:41
  • 3
    So actually, . is the original command and source is the synonym/alias for it.
    – helt
    Aug 2, 2017 at 16:02
398

Be careful! ./ and source are not quite the same.

  • ./script runs the script as an executable file, launching a new shell to run it
  • source script reads and executes commands from filename in the current shell environment

Note: ./script is not . script, but . script == source script

4
  • 36
    You are mixing up ./command and . script. source-command is same as .-command. Using ./meh says run script/binary named meh in the current directory, and got nothing to do with source/. -command. As explained in answer in your link. Aug 1, 2013 at 13:14
  • 5
    @JoakimElofsson It is mentioned in the link, but I will modify the answer to avoid missunderstand. Please correct it.
    – damphat
    Aug 2, 2013 at 11:45
  • 18
    It's kind of important that the accepted answer also points to this one, because for a moment I thought that ./ == source == .
    – Daniel F
    Dec 6, 2017 at 12:51
  • 1
    @JoakimElofsson 's comment was helpful but broke the parser in my brain a little bit.
    – Telos
    Apr 4, 2022 at 15:56
119

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.

4
  • 2
    Always know something new when reading man )
    – user83293
    Aug 15, 2013 at 8:41
  • 28
    You can also use help {builtin-name}, i.e. help source.
    – LawrenceC
    Mar 10, 2014 at 18:39
  • 3
    help doesn't work everywhere (atleast in zsh). type does.
    – kumarharsh
    Mar 28, 2014 at 8:35
  • 4
    To amplify: if you are using bash, and if you know (perhaps via 'type') it is a built-in command, then 'help' will get you directly to the paragraph of documentation you want without wading through 4,184 lines of 'man bash' text.
    – Ron Burk
    Apr 29, 2015 at 19:43
47

. (a period) is a bash shell built-in command that executes the commands from a file passed as argument, in the current shell. 'source' is a synonym for '.'.

From Bash man page:

. 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.
41

'source' is the long version of '.' command. On the bash prompt one can do:

source ~/.bashrc

to reload your (changed?) bash setting for current running bash.

Short version would be:

. ~/.bashrc

The man page:

. 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 executed from filename. If 
    filename does not contain a slash, file names in PATH are used to find the
    directory containing filename. 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 positional 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. 
0
37

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.

make_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

Execute

./make_alias

Verify

alias

Output

**nothing**

Whoops! Alias is gone with the new shell.

Let's go with the second option.

Option 2: source make_alias

Execute

source make_alias

or

. make_alias

Verify

alias

Output

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

Yeah Alias is set.

10

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

[root@abc ~]# info source

BASH BUILTIN COMMANDS
       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
              returned.

        .  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.
1
  • Does not work in zsh.
    – simon
    Oct 19, 2023 at 10:01
10

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
3
  • How does this answer differ from the 9 previous answers? Jun 12, 2017 at 0:31
  • 5
    I add another source of information and additional information not mentioned before. Jun 12, 2017 at 1:16
  • 2
    I didn't know that source could take arguments or use return.
    – Joe
    Aug 19, 2019 at 20:51
6

Type the command "help source" in your shell.

You will get output like this:

source: source filename [arguments]

Execute commands from a file in the current shell.

Read and execute commands from FILENAME in the current shell.  The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.

Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.
1
  • Does not work in zsh.
    – simon
    Oct 19, 2023 at 10:01
5

It should be noted that although being an awesome command, neither source nor its shorthand of . would source more than one file, meaning

source *.sh

or

. script1.sh script2.sh

will not work.

We can fall back using for loops, but it would issue the executable many times, creating multiple commands or issue of it.

Conclusion: source doesn't take multiple files as input. The argument has to be one.

Which sucks IMHO.

2

With source you can pass variables or functions from another file in to your script and use them without having to write them again.

F.I:

#!/bin/bash

source /etc/environment

source /myscripts/jetty-common/config/jetty-functions.sh

Cheers

0

Another example for source

create a test file

$ vi test.sh 
test()
{
echo Hello World
}

How do I print test function in terminal ?

  1. Convert it to executable
    $ chmod +x test.sh

  2. Perform source command
    $ source test.sh

  3. Run in terminal
    $ test

This will run test function in terminal

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .