What does 'source' do?

722,493

Solution 1

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]

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

'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. 
Share:
722,493

Related videos on Youtube

ob1
Author by

ob1

Updated on September 17, 2022

Comments

  • ob1
    ob1 almost 2 years
    $ 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?

    • bnjmn
      bnjmn over 10 years
      you forgot $ type source source is a shell built-in
    • arielnmz
      arielnmz almost 10 years
      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.
    • Scott
      Scott almost 10 years
      @lesmana, great link. That linked answer is the more thorough answer to this question.
    • Jasser
      Jasser almost 9 years
      Try "help source"
    • Thorbjørn Ravn Andersen
      Thorbjørn Ravn Andersen over 6 years
      source --help is a good start.
    • superboot
      superboot over 4 years
      $ help source Is the documentation command you are looking for. $ help will give you a list of the bash built-ins.
  • Assaf Levy
    Assaf Levy almost 15 years
    Is source a bash specific command or do other shells have it too? (I'm asking to get tags right on the question...)
  • Admin
    Admin almost 15 years
    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.
  • Joakim Elofsson
    Joakim Elofsson almost 11 years
    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.
  • damphat
    damphat almost 11 years
    @JoakimElofsson It is mentioned in the link, but I will modify the answer to avoid missunderstand. Please correct it.
  • jlliagre
    jlliagre almost 11 years
    @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.
  • Admin
    Admin almost 11 years
    Always know something new when reading man )
  • Rich Homolka
    Rich Homolka over 10 years
    @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.
  • LawrenceC
    LawrenceC over 10 years
    You can also use help {builtin-name}, i.e. help source.
  • LawrenceC
    LawrenceC over 10 years
    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.
  • kumarharsh
    kumarharsh over 10 years
    help doesn't work everywhere (atleast in zsh). type does.
  • Ron Burk
    Ron Burk about 9 years
    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.
  • Stephen Rauch
    Stephen Rauch about 7 years
    How does this answer differ from the 9 previous answers?
  • Alexandro de Oliveira
    Alexandro de Oliveira about 7 years
    I add another source of information and additional information not mentioned before.
  • helt
    helt almost 7 years
    So actually, . is the original command and source is the synonym/alias for it.
  • Daniel F
    Daniel F over 6 years
    It's kind of important that the accepted answer also points to this one, because for a moment I thought that ./ == source == .
  • user855443
    user855443 over 5 years
    This answer is at least misleading. Source and . is not synonym (see superuser.com/questions/46139/what-does-source-do). when myscript does set up an environment, ./myscript has no effect, but source myscript does.
  • jinawee
    jinawee about 5 years
    @user855443 The equivalent to source myscript is . myscript or . ./myscript if the script is in your current directory.
  • Joe
    Joe almost 5 years
    I didn't know that source could take arguments or use return.
  • Telos
    Telos about 2 years
    @JoakimElofsson 's comment was helpful but broke the parser in my brain a little bit.