0

Are there more ways of executing Unix binaries using a terminal (command line)? Using echo as an example:

$ /bin/echo "test"
test

$ cd /bin/
$ ./echo "test"
test

$ eval echo "test"
test
  • 2
    cd /; bin/echo "test", cd /usr/share; ../../var/log/../../bin/echo test. If we take your two first examples as distinct, then the possibilities are endless! – Sparhawk Nov 18 '18 at 03:56
  • do you mean "ways to execute it from a shell" or do you also mean to also include ways that are not from a shell, such as in a program that is not a shell script? – Skaperen Nov 18 '18 at 04:29
  • execute from a shell/terminal, yeah. ill update the question. your comment was very helpful actually. – user321532 Nov 18 '18 at 04:31
  • which shell are you using? there are many and each has their own ways. and there are an "infinite" number of ways to construct commands in each of the many shells. – Skaperen Nov 18 '18 at 04:48
  • i dont get how a similar question has been asked before and received +43 but i ask it (specific to binaries, not bash) and it gets -1? thanks anyway https://unix.stackexchange.com/questions/2976/ – user321532 Nov 18 '18 at 05:17
  • you probably got the -1 because there was a similar question asked before – Skaperen Nov 18 '18 at 06:10

2 Answers2

1

There many different ways to express code in a shell command line or in a shell script line that can result in a binary being executed. They all just come down to the point of running a shell command. The shell will try to run a file if the command refers to a file. That attempt will succeed if you have permission to execute the file. The file may be a binary executable or a script that refers to its interpreter on the first line (which begins with "#!") or the file may just be plain data like text. If it is executable at that point then it will run or be interpreted. The shell has plenty of ways to express a command. The possibilities are infinite.

Skaperen
  • 716
1

Please note that, as Sparhawk pointed out in the comments, the first two examples are doing basically the same (they invoke the program using its path instead of doing a path lookup) and following that logic the ways to execute a given program are virtually infinite.

Anyway, I'd say there are at least 4 different ways to execute programs[1] from a shell:

  • Shell builtins: some commands, such as echo itself in Bash, are implemented within the shell so the corresponding external utility, if exists, is not invoked.

    $ echo foo
    foo
    
  • Path lookup: when the shell searches the desired program in each of the directories included in the PATH environment variable.

    $ cat <<< foo
    foo
    
  • Direct invoking: when the program is invoked using its full/relative path.

    $ /bin/echo foo
    foo
    
    $ ../../bin/echo foo
    foo
    
  • Indirect invoking: when the program is invoked by another program (either a builtin or an external one).

    $ eval echo foo
    foo
    
    $ command echo foo
    foo
    
    $ sh -c 'echo foo'
    foo
    

1. I know you said binaries. I used a more general term because scripts can also be executed from a shell.

nxnev
  • 3,654
  • 1
    a reference to a file to be executed works the same whether the file is a script or a binary. except for sourcing the shell doesn't generally know or care. – Skaperen Nov 18 '18 at 06:15