Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  10] [ 0]  / answers: 1 / hits: 57443  / 2 Years ago, tue, february 15, 2022, 5:36:44

If <script/binary> is either a script or an executable binary file, which of the following commands can be used to execute it? If there are more than one way, how do they differ?


$ <script/binary>
$ ./<script/binary>
$ sh <script/binary>
$ source <script/binary>
$ . <script/binary>

Are there further ways?


More From » bash

 Answers
6

The following commands are the same, a dot component means "current directory". To allow for being executed, the files need to have executable permissions:



path/to/binary
./path/to/binary


Note that if a path does not contain a slash, it is treated as a command (either a shell built-in or a program that is looked up in the $PATH environment variable).



The following are almost the same, they execute a shell script (not a binary!) in the current shell environment. A small difference between the two lines are described on this Unix.SE question.



. path/to/script
source path/to/script


Finally you mentioned sh script. Again, this only works for shell scripts and not binaries. You are basically executing the sh program with the script name as argument. In the case of sh, it just treats this argument as shell script and executes it.



For answers restricted to shellscripts, see Different ways to execute a shell script.


[#36821] Wednesday, February 16, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anatta

Total Points: 326
Total Questions: 128
Total Answers: 96

Location: Jordan
Member since Sun, Jun 26, 2022
2 Years ago
anatta questions
Sun, Jul 17, 22, 07:13, 2 Years ago
Sun, Jun 6, 21, 12:17, 3 Years ago
Sat, Jun 12, 21, 20:43, 3 Years ago
Thu, Jan 13, 22, 20:49, 2 Years ago
Sat, Jun 5, 21, 05:39, 3 Years ago
;