I am new in shells, so I stuck with this kind of question. What is the simplest way to differentiate those commands?
Asked
Active
Viewed 601 times
8
-
6Does this answer your question? What are the differences between executing shell scripts using "source file.sh", "./file.sh", "sh file.sh", ". ./file.sh"? – SwissCodeMen Aug 23 '20 at 21:57
1 Answers
12
for
t.sh
the shell will search thePATH
in order for a file namedt.sh
and execute it if it finds itfor
. t.sh
the shell will search thePATH
in order for a file namedt.sh
but source it if it finds it.In the case of the bash shell, the search behavior for sourced commands has additional considerations, as noted in
man bash
:
When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched.
for
/t.sh
the shell will look for filet.sh
in the filesystem root directory/
and attempt to execute itfor
./t.sh
the shell will look for filet.sh
in the shell's current working directory.
and attempt to execute it
See also

steeldriver
- 136,215
- 21
- 243
- 336
-
It may be worth mentioning that bash's built-in
.
behaves differently in that it looks for files in the working directory if no files were found inPATH
. – danzel Aug 23 '20 at 23:22 -
it might be worth also noting that for security reasons the current directory is frequently not in PATH, so one might have to do ./t.sh instead of t.sh frequently. – Gnudiff Aug 24 '20 at 07:25