Alicia Alicia - 1 year ago 31
Linux Question

Why i can excute a newly created file which is not executable?

In Ubuntu, The default umask on Ubuntu is

which means that newly created files are readable by everyone, but only writable by the owner, nobody can excute it.
In this case, i create a new file :

touch test.rb # Its content is: puts "hello world"
ls -l demo.rb # -rw-r--r--

Then i excute

ruby test.rb # output: "hello world"

Since the owner of the file does not have the "x" permission , then why I can successfully run the file ? or I have missed some knowledge about it ?

Answer Source

You are not executing the file as a binary. You are executing ruby binary with argument test.rb and it interprets the Ruby script. Therefore, only ruby binary needs execution privilage and not the script itself.

You can check the privileges of the binary by running stat (which ruby).

On the other hand if you place


on the top of your script and make it executable with chmod a+x test.rb you could then make Linux run it. The binfmt module of the kernel will check search for #! (called shebang) in the file and run the interpreter for you.

You can find this shebang in lot of the shell scripts. Nowadays it is common to put #!/usr/bin/env ruby or #!/usr/bin/env python in order to use interpreter binary in other location that is available on PATH variable like /usr/local/bin/ruby. Again env is just another binary program. It will run its argument as a program. The kernel will pass script as the parameter which will result in command /usr/bin/env ruby test.rb.