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?

1
  • 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. Commented Jun 19, 2014 at 16:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I forgot that it's an already complied file. Now it's clear, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.