I saw the command like ". test.sh
" in some shell source code,
but I don't know what it does. So, I have tried this.
And, that .sh
file is executed.
However, I don't understand how ".
" works. Can you explain?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityThe .
command (which has a synonym, source
, in bash
, but not in most other Bourne shell derivatives) reads the named file as part of the current shell.
Consider:
$ cat test.sh
export ENVVAR="Welcome"
echo $ENVVAR
$ echo $ENVVAR
$ test.sh
Welcome
$ echo $ENVVAR
$ . test.sh
Welcome
$ echo $ENVVAR
Welcome
$
NB: Cheat 1: I assume test.sh
is executable. Cheat 2: I assume test.sh
is in a directory on $PATH
.
It means that environment variables set in test.sh
affect the current shell. By contrast, executing a command without the .
does not affect the environment of the current shell. The .
mechanism is used when .profile
and related files are read, for example.
Note that .
looks for simple names (like test.sh
with no slash in it) on PATH, but the file only has to be readable; it does not have to be executable.
bash
is not in POSIX mode, .
/source
also checks the current directory if no file is found using PATH
.
dot
command pre-dates the source
command by years. It was in the original Bourne shell. The C shell had the source
command for the same job; bash
imported that as a synonym for .
some time later.
Mar 23, 2013 at 15:16