• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

" . " used in front of shell scripts

 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone please tell me why do we use " . " in front of any shell script.
For example if i have a shell script abc.sh

while executing it
how is A output different from B?
A) $ abc.sh

B) $ .abc.sh


Thanks....
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a space between the dot and the script name.

The dot is a shortcut for the "source" built-in command. "source" reads the commands in the shell script into the current shell and executes them. Executing a script directly (i.e., your option B) launches a separate instance of the shell, which then executes the script. In option "A", variables set by the script will persist in the current shell; in option "B", nothing that happens in the script will affect the current shell.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ernest...

can you please elaborate this more.....

i mean with some example...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if the script "myscript" contains just

export CLASSPATH=foo

Then if you run

% export CLASSPATH=bar
% . myscript
% echo $CLASSPATH

you'll see "foo" as the output. On the other hand, if you do

% export CLASSPATH=bar
% myscript
% echo $CLASSPATH

you'll see "bar".
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "source" function is used a lot for things like picking up configuration data for use by a calling script - especially in places like the /etc/init.d scripts in Red Hat/Fedora Linux where they designed the system to separate the updateable script code from the user/system-specific data.

By using a sourced script instead of a config file, the calling script is relieved of the need to parse the configuration definitions and assigne them to working variables - the shell does it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic