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?

2
  • 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?") Commented Feb 19, 2016 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?" Commented Feb 19, 2016 at 12:59

2 Answers 2

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.

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

Comments

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.

Comments

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.