简体   繁体   中英

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

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?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM