0

I've written the following in the command:

$ cat /bin/ls > blah
$ cat blah blah blah > bbb
$ chmod u+x bbb
$ ./bbb

And it printed all the file names in the current working directory.

My question is why? and why not 3 times?

Mickey
  • 480
  • 4
  • 13
  • You essentially made a copy of the `ls` executable, with a bunch of extra garbage at the end (extra copies of the executable). Then you ran the copy... which listed files and exited. – Dmitri Jun 19 '14 at 16:20

1 Answers1

2

Because the Linux executable file format (ELF) is not a script that you can copy-paste three times in a row to get the same result. To be more precise, the header contains a single entry point (think of it as the address of where int main() has been stored), which is where the instructions are read from. Once you reach the final return 0; or whatever, the program stops, even if there is more (nicely structured) binary garbage following in the binary file.

TL;DR: Don't forget - /bin/ls is a compiled binary and not a shell script.

Pavel
  • 7,436
  • 2
  • 29
  • 42