8

From this guide to Bash completion we learn that in order for Bash to perform auto-completion one must perform . /etc/etc/bash_completion.d/foobar (note the space after .) in order for Bash completion to work.

$ /etc/bash_completion.d/ssh
bash: /etc/bash_completion.d/ssh: Permission denied
$ . /etc/bash_completion.d/ssh
$ ls -l /etc/bash_completion.d | grep ssh
-rw-r--r-- 1 root root   297 Jan 28 18:04 ssh

Is . a shortcut for the source command? If not, then what is it? It is impossible to google for, man source returns nothing, and apropos source and info source give so much irrelevant information that I cannot tell if what I'm looking for is in there. How might I even begin to RTFM to find the answer to this question myself?

  • Read the bash man page as this is a built in command, search for the explanation for source, it is difficult to miss. – Anthon Feb 1 '15 at 8:36
  • 3
    "Is . a shortcut for the source command?" — it's really the other way around: . is the command, and source is the compatibility alias (for compatibility with csh). Note that source does not exist in standard POSIX bourne shell, only .. – Celada Feb 1 '15 at 9:27
  • See also unix.stackexchange.com/questions/182219 – JdeBP Feb 1 '15 at 10:43
  • Often when it's simple it remains hidden... It can be as simple as to write "help": give a look below ;) – Hastur Feb 1 '15 at 12:27
15

Yes . is identical to the source function.

As always the first reference is the man bash manual page where you can confirm your initial guess by searching for / source

...shell function or script executed with . or source...

is the first reference, but a bit further you find a section Shell Builtin Commands

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

That fact it is a bash builtin function is the reason source doesn't come with its own man page, which is why apropos failed.

  • Thank you, I never would have thought to perform man bash, but I will add that to my "try before posting" techniques. That page looks like it will take me months to review and comprehend fully, I'm in for a good time! – dotancohen Feb 1 '15 at 8:38
  • As always the first reference is man bash? I'm sure if I have a problem with ssh command that will not help me much. It is more the reference of last resort. – Anthon Feb 1 '15 at 8:40
  • If you're writing bash scripts and investigating auto-completion, yes it certainly would be. But that may be the voice of experience. – HBruijn Feb 1 '15 at 8:42
  • The dot command was in the Bourne shell, and is standardized in the POSIX shell. The source command was the analogue in the C shell, and got added to Bash (and is not standardized by POSIX). – Jonathan Leffler Feb 1 '15 at 15:05
  • for bash builtins you can use help, as in help source or help . – Carlos Campderrós Feb 2 '15 at 7:54
5

Your answers in brief:

  1. . absolute_path/mycommand source the script file mycommand that is in the directory absolute_path/. For further references read here
  2. Yes . and source are equivalent.
  3. When needed, ask help to bash shell itself. You will have an answer for built in commands.

Some words more
Often the most simple way is the most elusive too: we didn't think we can ask help to the shell itself, when commands are defined internally.
With type . and type source we can notice that those are built-in commands.

Hastur@Cthulhu:~> type . source  
. is a shell builtin  
source is a shell builtin

Once that we know it, with help we can have some quick information about them.
The command help without parameter from the prompt give us:

GNU bash, version 4.1.2(1)...
These shell commands are defined internally.
Type help to see this list. Type help name to find out more about the function name.
Use info bash to find out more about the shell in general.
Use man -k' orinfo' to find out more about commands not in this list.

Meanwhile with help . as well as help source you obtain the same identical help:

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.

"The obvious is that which is never seen until someone expresses it simply."K.Gibran

1

This dot "." means reading and executing commands from the filename argument in the current shell context. It is equivalent to source. This style is from Bourne shell, and please refer to http://ss64.com/bash/source.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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