0

I would like to ask you the differences between the following ways in script execution.

.  script.sh      
../script.sh
. ./script.sh

Thank you in advance.

2

1 Answer 1

5
   script.sh
 ./script.sh
../script.sh

All of the above have the shell ask the kernel to execute the given file, which doesn't need to be a shell script (regardless of the name). The first looks for the file in PATH, the second in the current directory (.), the third in the parent directory.

.    script.sh
.  ./script.sh
. ../script.sh
source    script.sh
source  ./script.sh
source ../script.sh

Both . and source ask the shell itself to open and read the given file as shell commands, executed in the current shell, almost as if the commands inside were just given on the shell command line. That is, e.g. all variable assignments made in the sourced script are visible in the shell afterwards, which is not the case for regular execution. source is non-standard synonym for .. The first again looks for the file in PATH, the second in the current directory, and the third in the parent directory. Similarly for the other three.

(Except in Zsh, source looks at the current directory first, unlike .; and in Bash, if the shopt setting sourcepath is disabled, neither . or source search the PATH.)

See:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .