1

Yesterday I ran into the git execute bit bash script quirk - the one that requires:

git update-index --add --chmod=+x scriptname.sh

and it seemed strange to me that it was even possible to get stuck in this situation. (Ie having created a script file that you don't have permission to run).

If I have created a shell script - surely I can run it under the permissions of the shell execute permissions. Why would it need it's own execute permission bit?

My question is: Why does a bash script require an execute bit if a windows batch script can just be executed?

Community
  • 1
  • 1
hawkeye
  • 34,745
  • 30
  • 150
  • 304
  • 4
    There really isn't all that much to it. It just means you can't accidentally run a file that isn't actually intended to be a script. It's a feature Windows hasn't chosen to implement, perhaps because it uses the file extension instead. (In fact, you could turn it around and say "why does Windows require me to rename my script to end in .bat, when a UNIX file can just be executed?") – Harry Johnston Feb 19 '16 at 10:21
  • The corollary to this question is *"Why does Windows require me to name my files with `.EXE` or `.BAT` before I am allowed to run them? Why can't I name my files as I wish?"* – Mark Setchell Feb 19 '16 at 12:59

2 Answers2

3

To run a script you have two options in unix like systems. First Option is to use a direct interpreter call with the script as parameter.

# run a bash script
bash test.sh

# run a python scripts
python test.py

The second option is mark your file as executable, with the execute bit and after a call like this ...

# sample bash
./test.sh

# sample python
./test.py

... your system tries to find the right interpreter for you. For this the first line 'shebang' of the script is used.

Bash example:

#!/bin/bash
# points to the installed bash interpreter - bash example

Python example:

#!/usr/bin/python
# points to the installed python interpreter

To your question windows only use the file extension to detect a executable file.

OkieOth
  • 3,604
  • 1
  • 19
  • 29
0

Well, Linux is not Windows. Linux/Unix file systems support the executable bit to distinguish executable from pure data files, and to control exec permissions for user|group|others. You can still run the script if you prefix it with the name of the shell/binary you want to start it with, but if you want to do ./scriptname.sh or execute it from the path it needs to be flagged as executable for you as the onwer|a group member|some other user, and for scripts usually the shebang in the first line that defines the interpreter to start the script with: #!/bin/bash.

Murphy
  • 3,827
  • 4
  • 21
  • 35