13

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?

3 Answers 3

10

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.

0
6

I'll try to answer my own question.

Executing:

$ file          # when 'file' is on the PATH, or is a built-in
$ ./file        # when 'file' is in the current directory
$ /home/me/file

file, a binary or a script, must have execute permissions set.

Execute commands in script with specified shell

$ sh script
$ bash script
$ zsh script

Sourcing

$ . script
$ source script
1
  • Nice question, but it seems for me you have a few misinterprets in your answer. They are: 1) ./ - is there for a security issue: stackoverflow.com/questions/18552241/…; 2) $ . script # execute a script in the current dir. - it does not - it looks for a Command named script. To execute script in cur dir you should do $ . ./script or $ source ./script
    – Rustam A.
    Jul 6, 2021 at 12:19
2

Here's a quick listing of commands. Note, when I mention PATH, I mean the directories containing programs that the system knows about; you find those with echo $PATH, and it will be something like: /home/mike/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Scripts

  • To execute a script in current working directory, use ./myscript.sh
  • To execute a script on another file, use (if it is in the current working directory), ./myscript.sh textfile.txt
  • Scripts can also be run with arguments; as explained in Rute (p. 68): myfile.sh dogs cats birds will output The first argument is: dogs, second argument is: cats, third argument is: birds because the content of this script after the shebang is: echo "The first argument is: $1, second argument is: $2, third argument is: $3"

  • To execute a script in another directory, use ~/Scripts/dogs.sh

  • To execute a script that the system knows about because it is in your bin folder in your home directory (just create it if it isn't there, as it will automatically be added to your PATH), just use scriptname
  • To execute a script you have installed, again just use its name, because it will be known to the system: for example, get_iplayer

Binaries

  • To run a binary that the system knows about because it is in $PATH, use the name of the program and any parameters, for example, vlc <stream url to open>
  • To test a binary you have compiled before installing to /usr/local/bin, or to keep a standalone program away from the system, use ~/<folder>/app/myprog
5
  • Thanks for info. Is this statement correct: To execute a script or binary not in PATH one simply specifies its path. The reason ./ is needed for a script in the current path is that just 'script.sh' would be interpreted as a command since it lacks at least one slash /.
    – Carl
    Jul 17, 2012 at 20:19
  • "To execute a script you have installed", what is a script that I "have installed"? Does this point say the same than the previous point?
    – Carl
    Jul 17, 2012 at 20:19
  • @Carl- your first comment is correct, but it is not true to say that my last two points about scripts are the same. In point 5 of the script section I was talking about scripts that the user has manually added to their bin folder in their home directory; in point 6 I was talking about scripts such as get_iplayer that they have installed from the repositories and as such always go into system folders, not into the user's home directory.
    – user76204
    Jul 17, 2012 at 20:31
  • Sorry but I still don't understand that, a script in ~/bin/ (which is in PATH) or in a system folder (which is in PATH) - how can there be a difference between them? Do they behave differently?
    – Carl
    Jul 17, 2012 at 21:39
  • I was just making a distinction between those scripts that the user owns and adds to in ~/bin and those that are owned by root in the system folders.
    – user76204
    Jul 17, 2012 at 21:51

You must log in to answer this question.

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