What does 'source' do?
$ 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?
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
https://askubuntu.com/questions/182012/is-there-a-difference-between-and-source-in-bash-after-all?lq=1
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.