Why not make .txt file executable instead of .sh file to run scripts?

There is no need for the file extension to match anything specific, as you correctly found out.

On Unix-like systems, file types are usually derived from the content of the file (i.e. the "magic number" or other characteristic structures in the first few bytes) and not from their name. You could also completely omit the extension, which is often done for executables.

Check the file command, it shows you the information it can find out about a file type from its content.

For an executable script, the system expects a so-called "shebang" in the first line, which looks e.g. like

#!/usr/bin/env python3

and indicates which program to run as interpreter with the script file as argument. If you execute a text file without such a shebang, it will use your default shell, i.e. Bash to try and interpret it.

So on Unix/Linux systems, file name extensions are mostly a hint (but no guarantee) for the human user to quickly recognize what to expect a specific file to contain. It's also a convention that can help e.g. finding files faster.

Note that there are some exceptions though, where the name and extension matters (e.g. some system config files which must follow a naming convention, or many image viewers and editors also require the extension to indicate the file type).

You can also have a look at Do file-extensions have any purpose (for the operating system)?


There is no such thing as a file "extension" or "type" in a UNIX/Linux file name.

As others have pointed out, the "type" may be found using the file command assuming that the relevant magic is available on your system. UNIX/Linux file names can normally contain any character available but it often helpful to use some form of convention for the name so that both humans and machines can make judgements as to content (and hence usage of the content).

As an example, I often use a comma as first character of the file name to indicate a temporary file rather than using something like the string ".tmp" at the end of the filename. The result is the same, a file containing data that is only required for a short time perhaps but the name does not require to have a "." within it nor the string "tmp". This can sometimes have advantages e.g. when parsing a list of file names. But it is MY convention and someone else may decide on another even using ".tmp" as their convention of a name for a temporary file.

So my answer to you is that there is no such thing as a UNIX/Linux file extension merely a set of conventions that typically look like the file extensions used in other operating systems and their file systems.