why is bash(.sh) script not executable by default

bash, linux, shell

Solution

Ultimately the answer to this question is that that's not what `touch` was designed to do.

In fact, `touch` was not even designed to create files; its primary purpose in life is to change file timestamps. It is merely a side effect of that purpose, and the fact that the designers decided to be a little more generous and create the target file if it doesn't already exist (and if you didn't provide the `-c` option), that it allows you to create files at all.

It should also be mentioned that there are other techniques that create files, such as redirection (`echo 'echo '\''I am a script.'\'';' >|script.sh;`). The act of file creation is a generic one, and the whole concept of a file is a generic one. A file is just a byte stream; what goes in the byte stream is unspecified at the file abstraction layer. As @AdamGent mentioned, Windows requires certain types of executable files to have certain extensions in order to be executed properly, but even in Windows, you can put executable code in non-executable-extensioned files, and you can put non-executable content in executable-extensioned files. There's no enforcement of file name / file content correspondence at the file layer.

All of that being said, it would often be a convenience if you could easily create a script in Unix that automatically has executable permission set. I've actually written a script to allow me to edit a new file in vim, and then have its permissions set to executable after write-quitting. The reason this potential convenience has not been standardized into a utility is likely related to the concern about security; you don't want people to accidentally make files executable, because that raises the risk of security holes.

You can always write your own program to create a file and make it executable, perhaps based on the extension of the file name.

Another thing that can be added here is that even shell scripts don't always need to be executable. For example, if you write a script that is only intended to be sourced from existing shell processes (via the `source` or classic `.` builtins), then the script does not need to be executable at all. Thus, there are cases where the file extension itself does not provide enough information to determine what the appropriate permissions are for the file.

Problem

Why is bash(.sh) script not executable by default. I agree that while we `touch` any file in linux it is created for reading purpose. But since file name extensions such as sh and csh are for execution purpose. Won't it be ideal to `touch` them in an executable mode. Question might sound redundant to, but i still thought of asking it :)

Original source