Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use a dot to execute the profile?

Tags:

unix

I'm new to Linux. In the command below, why do I need to use a dot to execute the profile?

. ~/.profile
like image 259
mko Avatar asked Feb 17 '11 14:02

mko


3 Answers

As Noufal mentioned, . is an alias for source.

By sourcing the file, all commands are executed within the context of your current bash session, which means that all environment variables which it exports will now be available to you.

If you run the script instead of source it, it is executed in a subshell and exported variables are not passed on to your session. In effect, that pretty much defeats the purpose of .profile.

As a demonstration, say you have the file test.sh:

#!/bin/bash
# in test.sh
print "exporting HELLO"
export HELLO="my name is Paul"

If you execute it:

[me@home]$ bash test.sh
exporting HELLO
[me@home]$ echo $HELLO

Nothing gets printed out since $HELLO is not defined in your current session. However, if you source it:

[me@home]$ . test.sh
exporting HELLO
[me@home]$ echo $HELLO
my name is Paul

Then $HELLO will be available in your current session.

like image 78
Shawn Chin Avatar answered Oct 29 '22 17:10

Shawn Chin


The period operator is an alias for the source command. Details here.

like image 28
Noufal Ibrahim Avatar answered Oct 29 '22 15:10

Noufal Ibrahim


Pretty hard to tell without more context, but one usage is the Bash-specific file .bash_profile to include the more generic (as far as Bourne shells go) file .profile, since when Bash finds the first, it won't load the second one by itself.

like image 1
js. Avatar answered Oct 29 '22 15:10

js.