Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
$ 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?

share|improve this question
    
related: superuser.com/questions/176783/… –  lesmana Jan 17 '11 at 21:59
4  
you forgot $ type source source is a shell built-in –  bnjmn Oct 9 '13 at 6:00

7 Answers 7

up vote 80 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]
share|improve this answer
1  
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
4  
@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
1  
@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 at 18:31
    
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. –  ultrasawblade Mar 10 at 18:41

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.

share|improve this answer
1  
Always know something new when reading man ) –  user83293 Aug 15 '13 at 8:41
2  
You can also use help {builtin-name}, i.e. help source. –  ultrasawblade Mar 10 at 18:39
1  
help doesn't work everywhere (atleast in zsh). type does. –  Kumar Harsh Mar 28 at 8:35

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" Read and execute commands from filename in the current shell environment

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

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

share|improve this answer
7  
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

. (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.
share|improve this answer

'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. 
share|improve this answer

This is a shell built-in command, read the manual for your shell.

which source

man bash
share|improve this answer
2  
Mentioning "shell built-in command" is good, but this answer would be better if combined with that of Jawa. :) –  Jonik Sep 24 '09 at 11:19

The man pages complicates the definition. In Layman's Terms...

"./script.sh" runs the shell script in the foreground (current environment or existing/current process)

"source script.sh" runs the shell script in the background (new environment or new/background process)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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