17

I recently learned, that

. ./.a.a and ./.a.a is the same.

However trying source source .a.a gives an error. IMO, . being Bash alias for source shouldn't behave differently, so what am I missing?

Bonus, why is . . OK while source source is not?

  • 8
    You could have chosen a clearer name for your example :). In any case, whatever gave you the idea that . script and ./script are the same thing? They most certainly are not. Also, . is not an alias to source. It's the other way around. . is the standard and source is an alias of .. – terdon Sep 14 '16 at 9:27
  • 2
    After spending some time looking at shell scripts, you get used to the . command, but I mean source is just such a better command, when you see source somefile in a script you immediately know what that statement does, but . somefile not only is not obvious what it does, you can easily miss the . character altogether – the_velour_fog Sep 14 '16 at 9:47
  • Bonus, why is . . OK while source source is not? - in how far the error message bash: .: .: is a directory on . . is ok needs to be explained. – countermode Sep 14 '16 at 10:25
  • @terdon - absolutely right. The original example was . ./.a.a . to be honest. :-D A quiz of sorts, if you will. :-) Linked question is awesome, many thanks. What gave me that idea: the fact that I had seen no difference in executing files this way and the fact I read somewhere . is an alias for source. :-) – LAFK says Reinstate Monica Sep 14 '16 at 16:14
27

You can't just replace . with source everywhere; if

. ./.a.a

works, you can replace the first . (at least in Bash):

source ./.a.a

The second . represents the current directory, you can't replace that with source (especially not ./ with source as you've done).

source source

would be OK if you had a file called source in the current directory, containing something meaningful for your current shell. I can't see how . . would be OK...

Also, . ./.a.a and ./.a.a aren't the same, the second form runs .a.a in a separate shell. See What is the difference between sourcing ('.' or 'source') and executing a file in bash? for details.

|improve this answer|||||
9

source is a shell keyword that is supposed to be used like this: sourcefile where file contains valid shell commands. These shell commands will be executed in the current shell as if typed from the command line. Now, .file does exactly the same.

Beyond that . alone means "the current working directory" as in ./xyz ("xyz in this directory") or a/b/./c/./d (which is identical to a/b/c/d).

Beyond that . in a filename has a meaning only by convention as in .foobar which indicates a "hidden" file (not really...) or as in foobar.pdf, which indicates a file format by the suffix (here .pdf).

These different meanings cannot be interchanged.

|improve this answer|||||
  • 8
    . is in no way outdated. It, unlike source is the portable, POSIX-defined way of sourcing a file. The source alias is shell-specific and can be either absent (for example in dash) or behave differently from the standard .. – terdon Sep 14 '16 at 16:25
  • Thanks @terdon, Wiki says that the source as a dot (.) is not acceptable in C shell, where the command first appeared. – Noam Manos Mar 15 '18 at 10:07
  • 1
    @NoamManos perhaps, but the C-shell is not POSIX, so it can (and does) behave very differently. – terdon Mar 15 '18 at 10:10

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.