10

EDIT : actually, it is not an alias (see answers)

As you all know, in a shell, the dot-command (.) is an alias to the source command.

But I wonder if there's a reason behind such a weird alias? Apparently, I don't use it so often that I would need such a short alias.

So, why a dot? And why for source and not a more commonly used command such as cd or ls? Why even an alias? Is there a good reason behind that? Or is there a historical reason?

NOTE: I originally posted this question on Server Fault but I was suggested to post it here instead.

slm
  • 369,824
  • 5
    You actually have it backwards – jesse_b Jul 28 '18 at 19:51
  • 2
    . is not an alias in the first place; nor is it a secondary name. This question takes a falsehood as its premise and, as demonstrated by the answers so far, is unanswerable except to say that the question is wrong, as such loaded questions are. – JdeBP Jul 28 '18 at 22:11
  • @JdeBP Actually, I tried alias source and alias . in order to know which one was the alias of the other but got no response from the command line. Now I understand why. Hence, I supposed that . was the alias because it was the shortest. I edited the question to make it clearer that the question is actually wrong. I did not change the title because it would change the meaning of the answers. – Arnaud Denoyelle Jul 28 '18 at 22:16
  • The next question can be found in the list that I collected two years ago at https://superuser.com/questions/1136409/#comment1631506_1136409 . (-: – JdeBP Jul 28 '18 at 22:29
  • Possible duplicate - https://unix.stackexchange.com/questions/58514/what-is-the-difference-between-and-source-in-shells – slm Jul 29 '18 at 02:54
  • BTW, there's also retrocomputing.stackexchange.com, which may be better suited to historical questions. – dirkt Jul 29 '18 at 05:04
  • We take historical questions here. We certainly give answers that go into the history of things. (-: – JdeBP Jul 29 '18 at 07:36

3 Answers3

14

. is the POSIX standard.

source is a bash builtin synonym for . and is not as portable as .

Also note in the Bash reference manual . is listed under 4.1 Bourne Shell Builtins and source is listed under 4.2 Bash Builtin Commands as:

A synonym for . (see Bourne Shell Builtins).

It's possible that bash named it source because that's what it was called in C shell (where the command apparently originated).

jesse_b
  • 37,005
  • 1
    Your answer is not correct, sorry. Dot is from the Bourne Shell and source is from csh, see my answer. – schily Jul 28 '18 at 21:45
  • 2
    @schily: Where did I say something contrary? – jesse_b Jul 28 '18 at 21:46
  • You cannot explain something introduced in 1977 by pointing to a software that was started in 1989. – schily Jul 28 '18 at 21:48
  • I really don't know what you are talking about – jesse_b Jul 28 '18 at 21:48
  • 1
    @Jesse_b: he's saying that POSIX was introduced in the eighties, but dot predates that by a decade or so. So if you want to know why dot and not something else, POSIX doesn't explain that. Right, everybody? Actually I don't think schily's answer answers the question either, as written: it doesn't say anything about why "." was chosen rather than some other name. – Croad Langshan Jul 28 '18 at 21:57
  • @CroadLangshan: That sort of makes sense. Although I am not trying to explain the origin of ., I in fact have another question asking about it. OP has several questions in this question and I'm simply pointing out that source is the alias to . and not the other way around. My answer is to the question in the title "Why is dot-command an alias for source?" (answer: It's not) – jesse_b Jul 28 '18 at 21:59
  • POSIX-1988 (the first) only mentions libc. POSIX-1992 mentions the utilities and the shell. Bash is from 1989. For the "why .", you need to ask Stephen Bourne or one from the people that discussed the concepts for the Bourne Shell with Stephen Bourne. IIRC Stephen Bourne did not mention dot in his talk on the beginning of sh. – schily Jul 28 '18 at 22:01
  • "Why dot?" is a different question, Croad Langshan, to be found at https://unix.stackexchange.com/questions/459099/ . This question is why . is an alias, which it isn't. As such, it is a loaded question that takes a falsehood as its premise. – JdeBP Jul 28 '18 at 22:03
  • 1
    Really seems the primary question here is So, why a dot?, and your other question is a duplicate. I'm genuinely not sure which if any of the subsidiary questions (which I think you would have better chosen to comment on or ignore) you're answering here... – Croad Langshan Jul 28 '18 at 22:04
  • @CroadLangshan: Technically asking multiple questions at once is off topic but you have a fair point. – jesse_b Jul 28 '18 at 22:04
  • 1
    @JdeBP yes I saw the other question, I just think you both misinterpreted this one. – Croad Langshan Jul 28 '18 at 22:05
  • No, you did. The questioner directly asks why . is an alias, using those very words. Three times. – JdeBP Jul 28 '18 at 22:13
  • 1
    You all answered the question by pointing out that my understanding was wrong. I now understand that this is not an alias. I follow the second question in order to know why the name . :) – Arnaud Denoyelle Jul 28 '18 at 22:24
5

As far as Bash is concerned the . and source are not aliases for one or the other, it's a simple fact that they're both builtins which call the same underlying function, source_builtin.

Take a look at the source.def file from the Bash source code:

$PRODUCES source.c

$BUILTIN source
$FUNCTION source_builtin
$SHORT_DOC 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.
$END

$BUILTIN .
$DOCNAME dot
$FUNCTION source_builtin
$SHORT_DOC . 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.
$END

References

slm
  • 369,824
2

The dot command was introduced by the Bourne Shell most likely in 1976.

The source command was introduced by the csh in 1977 or 1978.

So there is not an alias relation but two different names for an "invention" at the same time.

BTW: I can tell you why cd is named this way. The command previously had been called chdir, but this was too long (slow to type) for the upcoming 110 Baud modems in 1974...

slm
  • 369,824
schily
  • 19,173