source
command executes the provided script (executable permission is not mandatory) in the current shell environment, while ./
executes the provided executable script in a new shell.
source
command do have a synonym . filename
.
To make it more clear, have a look at the following script, which sets the alias.
make_alias
#! /bin/bash
alias myproject='cd ~/Documents/Projects/2015/NewProject'
Now we have two choices to execute this script. But with only one option, the desired alias for current shell can be created among these two options.
Option 1: ./make_alias
Make script executable first.
chmod +x make_alias
Execute
./make_alias
Verify
alias
Output
**nothing**
Whoops! Alias is gone with the new shell.
Let's go with the second option.
Option 2: source make_alias
Execute
source make_alias
or
. make_alias
Verify
alias
Output
alias myproject='cd ~/Documents/Projects/2015/NewProject'
Yeah Alias is set.
2My shell returned this
$ whatis source
source (1) - bash built-in commands, see bash(1)
. Also,man source
takes me to theBASH_BUILTINS(1)
man pages. This is on Fedora btw, no idea why those debian packages are un-(or badly)-documented. – arielnmz – 2014-08-20T12:29:58.7605
@lesmana, great link. That linked answer is the more thorough answer to this question.
– Scott – 2014-09-05T15:02:59.2006Try "help source" – Jasser – 2015-09-19T14:12:20.853
source --help
is a good start. – Thorbjørn Ravn Andersen – 2018-03-13T16:45:01.8606
related: http://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-script/176788#176788
– lesmana – 2011-01-17T21:59:56.91761you forgot
$ type source
source is a shell built-in
– bnjmn – 2013-10-09T06:00:22.047