What does 'source' do?
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.
Related videos on Youtube
ob1
Updated on September 17, 2022Comments
-
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 over 10 yearsyou forgot
$ type source
source is a shell built-in
-
arielnmz almost 10 yearsMy shell returned this
$ whatis source
source (1) - bash built-in commands, see bash(1)
. Also,man source
takes me to theBASH_BUILTINS(1)
man pages. This is on Fedora btw, no idea why those debian packages are un-(or badly)-documented. -
Scott almost 10 years@lesmana, great link. That linked answer is the more thorough answer to this question.
-
Jasser almost 9 yearsTry "help source"
-
Thorbjørn Ravn Andersen over 6 years
source --help
is a good start. -
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 almost 15 yearsIs
source
a bash specific command or do other shells have it too? (I'm asking to get tags right on the question...) -
Admin almost 15 yearsAfaik,
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 thesource
command, less certain about which shells do contain it. -
Joakim Elofsson almost 11 yearsYou 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 almost 11 years@JoakimElofsson It is mentioned in the link, but I will modify the answer to avoid missunderstand. Please correct it.
-
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 usesource
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 almost 11 yearsAlways know something new when reading
man
) -
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 over 10 yearsYou can also use
help {builtin-name}
, i.e.help source
. -
LawrenceC over 10 yearsOne 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 thesource
, or at least check for valid values. -
kumarharsh over 10 years
help
doesn't work everywhere (atleast in zsh).type
does. -
Ron Burk about 9 yearsTo 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 about 7 yearsHow does this answer differ from the 9 previous answers?
-
Alexandro de Oliveira about 7 yearsI add another source of information and additional information not mentioned before.
-
helt almost 7 yearsSo actually,
.
is the original command andsource
is the synonym/alias for it. -
Daniel F over 6 yearsIt's kind of important that the accepted answer also points to this one, because for a moment I thought that
./ == source == .
-
user855443 over 5 yearsThis 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 about 5 years@user855443 The equivalent to
source myscript
is. myscript
or. ./myscript
if the script is in your current directory. -
Joe almost 5 yearsI didn't know that
source
could take arguments or usereturn
. -
Telos about 2 years@JoakimElofsson 's comment was helpful but broke the parser in my brain a little bit.