$ 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?

  • 4
    related: superuser.com/questions/176783/… – lesmana Jan 17 '11 at 21:59
  • 46
    you forgot $ type source source is a shell built-in – bnjmn Oct 9 '13 at 6:00
  • 1
    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 '14 at 12:29
  • 3
    @lesmana, great link. That linked answer is the more thorough answer to this question. – Scott Sep 5 '14 at 15:02
  • 1
    Try "help source" – Jasser Sep 19 '15 at 14:12

11 Answers 11

up vote 420 down vote accepted

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

Syntax

. filename [arguments]

source filename [arguments]
  • 6
    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 '09 at 11:17
  • 2
    Afaik, source was present in the Bourne shell and hence probably present in all its descendants. en.wikipedia.org/wiki/Bourne_shell. I know that not all shells have the source command, less certain about which shells do contain it. – nagul Sep 24 '09 at 11:47
  • 11
    @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 '13 at 14:02
  • 16
    @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. – Rich Homolka Mar 10 '14 at 18:31
  • 3
    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 '14 at 18:41

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

https://askubuntu.com/questions/182012/is-there-a-difference-between-and-source-in-bash-after-all?lq=1

  • 24
    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. – Joakim Elofsson Aug 1 '13 at 13:14
  • 2
    @JoakimElofsson It is mentioned in the link, but I will modify the answer to avoid missunderstand. Please correct it. – damphat Aug 2 '13 at 11:45
  • 1
    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 '17 at 12:51

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.

  • 1
    Always know something new when reading man ) – user83293 Aug 15 '13 at 8:41
  • 17
    You can also use help {builtin-name}, i.e. help source. – LawrenceC Mar 10 '14 at 18:39
  • 1
    help doesn't work everywhere (atleast in zsh). type does. – kumar_harsh Mar 28 '14 at 8:35
  • 2
    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 '15 at 19:43

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

'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 short
    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. 
  • This should be the accepted answer. – Peter Mortensen Mar 19 '17 at 8:00

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.

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.
  • Could you provide more than just RTFM? – Peter Mortensen Mar 19 '17 at 9:04

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.

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
  • How does this answer differ from the 9 previous answers? – Stephen Rauch Jun 12 '17 at 0:31
  • 1
    I add another source of information and additional information not mentioned before. – Alexandro de Oliveira Jun 12 '17 at 1:16

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.

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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