Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I can seem to run scripts (.sh) with and without them being set as executable. So where exactly this matters?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

Let's say you have the file myscript containing the following:

#!/bin/bash
echo "Hello, World!"

If you make this file executable and run it with ./myscript, then the kernel will see that the first two bytes are #!, which means it's a script-file. The kernel will then use the rest of the line as the interpreter, and pass the file as its first argument. So, it runs:

/bin/bash myscript

and bash reads the file and executes the commands it contains.

Thus, for bash (or whatever interpreter your script requires) to "execute" the script, it only needs to be able to read the file.

So, for scripts, the execute bit just makes it a bit more convenient to execute it. As long as bash is executable, you can always run bash with the script file as argument, or run bash interactively and copy paste the script line by line into your terminal to have the commands executed.

share|improve this answer
    
Thanks for explaining in detail. :) If I understood it right, any user access to bash just needs read access to the script to run it because bash will take the script as input and will only need to read it. I can stop this behaviour by taking the read permission of the script for that user. Right? So when does this executable permission is used & exactly matters? –  Ashfame Feb 10 '11 at 20:02
    
Yes, if you use "bash myscript" then user only need read access for "myscript" (and of course executable for /bin/bash if bash is there within the path). However, if you make your script executable, things are a bit different. It's true that from the kernel's point of view, it will be "/bin/bash myscript" again if the first line is #!/bin/bash, but to reach that point, first you need to present the script as executable for the user or group. However, that's true if for some user the script is not executable but readable, (s)he can still "run" the script with "bash myscript" .... –  LGB Feb 10 '11 at 21:40
    
@LGB So that's pretty much useless. Good thing is to cut the read permission away from a user for that script. Right? –  Ashfame Feb 10 '11 at 22:08
    
@Ashfame: I would say useful rather than useless. The execute bit on regular files is not (and never was intended to be) used for limiting access. It seems like you're expecting that from it. If you have a binary executable, you must have execute permission on it to run it. However, if you have read access to it, you can always copy the file to your own home directory, in which case the copy will be owned by you, so you can add execute permission to it ... and run it. For directories though, it has a bit different purpose. See mywiki.wooledge.org/Permissions –  geirha Feb 10 '11 at 22:21

Make sure you are not confusing "executing the shell script" with "run a shell script using sh".

This will not be affected by file permissions on file.sh:

sh file.sh

You are executing sh (which resolves to the program /bin/sh), which reads file.sh and executes it's code.

File permissions will have effect if you really execute the script itself :

./file.sh

Note that file permissions are not supported by non-Linux filesystems, like FAT. So even if you run chmod -x file.sh, the file will still have it's former permissions.

Execute permission is enforced by the filesystem. But programs can "execute" the code too by reading the file contents, which bypasses filesystem permissions on "execute".

share|improve this answer
    
I understand your point. But then is it for group or world or both? –  Ashfame Feb 10 '11 at 19:04
    
@Ashfame If you set the executable permission, the script can be run directly by users who have that permission - whether they have it on a group, world or owner basis. But even people without execute permission can execute it by calling a different program (like bash) to do the execution - to block that you would have to take away their read permission as well. –  j-g-faustus Feb 10 '11 at 19:37
    
If you set the executable permission, the script can be run directly by users who have that permission - whether they have it on a group, world or owner basis But how the permission is granted to different users by checking the executable permission? And I got your second point. You mean taking away their read permission of the script so that they can't even process it through bash. Right? –  Ashfame Feb 10 '11 at 19:58
    
@Ashfame On the read permission point, yes. On how, you would call something like sudo chmod g+x myfile.sh in the terminal to add execute permissions for the file's group. See File permissions tutorial. To manage permissions for several users simultaneously, you would use groups, see for example Managing groups. –  j-g-faustus Feb 10 '11 at 21:43
    
what i meant was when we add the execuatable permission in GUI, then its for whom? owner/group/world? –  Ashfame Feb 10 '11 at 21:57

Don't think of it in the way that. Can I execute this file? Think of it in the way that: Who can execute this file?

If the computer is yours and the file is yours I am sure you can execute it. You might want to look further into commands like chmod and chown, and file permissions.

I hope that helps.

share|improve this answer
    
Yes I know about them. That means, I (owner) can always execute it. Right? Then setting a file as executable is for the group or world or both? –  Ashfame Feb 10 '11 at 19:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.