5

What does . /path/to/a/shell-script-file do exactly? I mean obviously it executes that shell script but why put that . followed by a space before the path/name of the script file?

Wildcard
  • 35,316
  • 26
  • 130
  • 258
Andy O.
  • 91
  • 4
  • I missed the space after the period, so my answer was totally inaccurate and I deleted it. – Wildcard Oct 12 '15 at 02:13
  • Possible duplicate of [Different ways to execute a shell script](http://unix.stackexchange.com/q/2976/80216)  Related: [running a script with “.” and with “source”](http://unix.stackexchange.com/q/17815/80216). – G-Man Says 'Reinstate Monica' Oct 12 '15 at 03:20

2 Answers2

10

. or source tells the shell to execute the script itself, instead of forking a sub-shell to run it in.

This allows the script to modify the environment of the shell.

For example, if you have a script that sets certain environment variables or defines aliases then running it without . will define those things in a sub-shell, and they will disappear when the sub-shell terminates. Running it with . will define them in the current shell.

cas
  • 1
  • 7
  • 119
  • 185
1

Say you have a file of custom environment settings (aliases, additions to $PATH, etc). Call the file custom_env.

Usually this stuff will go in your .profile or .bashrc. But sometimes you might want it in separate file, then you can apply it to your session as needed, e.g.

. ./custom_env
roblogic
  • 207
  • 2
  • 9