What does 'source' do?

604

185

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

Andrea Ambu

Posted 2009-09-24T10:35:48.933

Reputation: 6 670

2My 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 – 2014-08-20T12:29:58.760

5

@lesmana, great link. That linked answer is the more thorough answer to this question.

– Scott – 2014-09-05T15:02:59.200

6Try "help source" – Jasser – 2015-09-19T14:12:20.853

source --help is a good start. – Thorbjørn Ravn Andersen – 2018-03-13T16:45:01.860

61you forgot $ type source source is a shell built-in – bnjmn – 2013-10-09T06:00:22.047

Answers

486

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]

user4358

Posted 2009-09-24T10:35:48.933

Reputation:

2So actually, . is the original command and source is the synonym/alias for it. – helt – 2017-08-02T16:02:46.053

This answer is at least misleading. Source and . is not synonym (see https://superuser.com/questions/46139/what-does-source-do). when myscript does set up an environment, ./myscript has no effect, but source myscript does.

– user855443 – 2019-01-08T07:41:26.863

1@user855443 The equivalent to source myscript is . myscript or . ./myscript if the script is in your current directory. – jinawee – 2019-04-22T08:08:42.497

8Is source a bash specific command or do other shells have it too? (I'm asking to get tags right on the question...) – Jonik – 2009-09-24T11:17:21.767

2

Afaik, source was present in the Bourne shell and hence probably present in all its descendants. http://en.wikipedia.org/wiki/Bourne_shell. I know that not all shells have the source command, less certain about which shells do contain it.

– None – 2009-09-24T11:47:12.427

14@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 – 2013-08-02T14:02:30.923

20@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 – 2014-03-10T18:31:32.090

3One 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 – 2014-03-10T18:41:47.470

289

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

damphat

Posted 2009-09-24T10:35:48.933

Reputation: 3 023

4It's kind of important that the accepted answer also points to this one, because for a moment I thought that ./ == source == . – Daniel F – 2017-12-06T12:51:44.163

27You 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 – 2013-08-01T13:14:23.780

3@JoakimElofsson It is mentioned in the link, but I will modify the answer to avoid missunderstand. Please correct it. – damphat – 2013-08-02T11:45:17.347

92

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.

micans

Posted 2009-09-24T10:35:48.933

Reputation: 1 049

4To 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 – 2015-04-29T19:43:01.507

1Always know something new when reading man ) – None – 2013-08-15T08:41:43.247

20You can also use help {builtin-name}, i.e. help source. – LawrenceC – 2014-03-10T18:39:32.243

1help doesn't work everywhere (atleast in zsh). type does. – kumarharsh – 2014-03-28T08:35:59.153

40

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

Jawa

Posted 2009-09-24T10:35:48.933

Reputation: 3 349

32

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

Joakim Elofsson

Posted 2009-09-24T10:35:48.933

Reputation: 2 056

This should be the accepted answer. – Peter Mortensen – 2017-03-19T08:00:01.363

28

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.

Harsh Vakharia

Posted 2009-09-24T10:35:48.933

Reputation: 401

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.

Akshay Upadhyaya

Posted 2009-09-24T10:35:48.933

Reputation: 111

Could you provide more than just RTFM? – Peter Mortensen – 2017-03-19T09:04:58.473

5

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.

Jasser

Posted 2009-09-24T10:35:48.933

Reputation: 151

4

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

Alexandro de Oliveira

Posted 2009-09-24T10:35:48.933

Reputation: 163

How does this answer differ from the 9 previous answers? – Stephen Rauch – 2017-06-12T00:31:21.407

2I add another source of information and additional information not mentioned before. – Alexandro de Oliveira – 2017-06-12T01:16:23.087

1I didn't know that source could take arguments or use return. – Joe – 2019-08-19T20:51:41.940

2

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.

user373230

Posted 2009-09-24T10:35:48.933

Reputation:

0

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

DimiDak

Posted 2009-09-24T10:35:48.933

Reputation: 111