1

This is a pretty simple question I'm sure, but I'm having trouble dredging up a sensible answer via google as it's rather hard to search for (or at least, I don't know the right terminology).

I see a period (i.e. 'dot', aka this guy> '.') being used in scripts and I think of it as basically being expanded to mean 'pwd', i.e. defining the current working path (as per the output of ls -la for instance). But, there is clearly more to it, for instance, here's two examples where I don't understand how the '.' is being used and what it really means:

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

And also this one:

. <(cat /proc/32684/environ |xargs -0 -i echo {} |grep SSH)

(Side note: This is from an interesting article on ssh-agent: http://rabexc.org/posts/pitfalls-of-ssh-agents)

Looking at the context of these uses, the period appears to represent 'the current process' or something like that (perhaps the current shell?) and the loading of the output of a file or command substitution into it?

It would be nice to know the answer though, rather than continuing to guess and suppose :-)

EDIT: A bonus question, in the same way that '#!' is a shebang / hash-bang / sharp-bang etc, is there a term/name for the dot operator/shortcut?

Chris
  • 315

2 Answers2

1

It is to run the given file if it exists .

. <(ls) For example will run all the files in directory . Name is dot . This is what i found online . https://pubs.opengroup.org/onlinepubs/009695399/utilities/dot.html But honestly thou , im a noob and i just signed up today , lol . I am sure we will see alot of our geniuses answering it soon .

Ragbir
  • 26
  • How does that make sense in the second example though where it's receiving a command substitution, is it then executing the result of the substitution? – Chris Aug 22 '20 at 06:37
  • 1
    @Chris A process substitution (not "command substitution") resolves into the name of a file that holds the result of the given command. – Kusalananda Aug 22 '20 at 06:47
  • I get the use of that in the first case. I didn't realise you could seperate the dot from the target with a space...interesting. So in the first example the dot is working exactly the same as it does with ./thisProgram where it is used to run 'thisProgram' from within the current working directory? – Chris Aug 22 '20 at 06:48
  • Thanks @Kusalananda, that would then make sense in the second example as well. It was seeing it spaced out from the 'target' to be executed that made me think it must be something different. Thanks all! – Chris Aug 22 '20 at 06:50
0

In summary:

  1. This is known as 'Dot notation'.
  2. The command source is an alias for the dot command i.e. for .

There is some explanation in the links from Prvt_Yadav (https://stackoverflow.com/q/16011245/9235408) and Weiland (https://unix.stackexchange.com/a/114306/266125), but to answer the question here:

Using . ./ (dot space dot slash) ....... will execute the script in the current shell without forking a sub shell.

Answer: https://stackoverflow.com/a/16011496/2927555

Use dot notation to source in the script file in the current shell i.e. without creating a sub-shell

Answer: https://stackoverflow.com/a/16011414/2927555

Chris
  • 315