简体   繁体   中英

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

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 :)

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.

Because the .sh script extension is completely arbitrary in Unix environments. Its only a convention. You can name your script whatever you like so long as it has an executable bit set. This is unlike Windows where I believe its required ( .com , .exe , and I think .dll ).

touch just changes the timestamp of the file. It again does not care what the file extension of the file is. In fact most tools in Unix do not care about file extension.

There is nothing in the file name that says a file is even a script. Common practice perhaps says that .sh and .csh are scripts but there is no such rule.

What makes a file an executable script is the magic number in the beginning of the file. The magic number #! (the shebang, which has many other names as well) means the file is a script. For example

#!/bin/bash

When you try to execute the file (it must then also be set to executable in the permissions), the Linux kernel looks at the first two bytes of the file and finds the magic number (#!). It then knows it is supposed to execute whatever comes after the Shebang with the name of the file as argument followed by the rest of the arguments passed.

If you type the following in the shell

blablabla.sh lol1 lol2

The shell recognizes that you are trying to execute something so it invokes the kernel

exec blablabla.sh lol1 lol2

The kernel finds the shebang, and it becomes

exec /bin/bash blablabla.sh lol1 lol2

By exec i mean one of the exec family system calls.

Other fun names for #! include sha-bang, hashbang, pound-bang, hash-exclam and hash-pling

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