On wikipedia, the article for .sh says:
For the .sh file extension type, see Bourne shell.
How about other unix shells?
I know that the shebang is used inside the file to indicate an interpreter for execution, but I wonder:
- What are the pros and cons for files extensions vs no file extensions?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I would only call .sh something that is meant to be portable (and hopefully is portable).
Otherwise I think it’s just better to hide the language. The careful reader will find it in the shebang line anyway. (In practice, .bash or .zsh, etc… suffixes are rarely used.)
Method 2
I would say that no “good practices” for file extensions exist, strictly on a technicality: Unix/Linux/*BSD file systems don’t support extensions per se. What you are calling an extension is merely a suffix of a single file name. That’s different than the VM/CMS, VMS, MS-DOS and Windows file systems and OSes where a special spot in the inode-moral-equivalent is reserved for an extension.
That little rant now over, I think it’s a bit silly to put a “.sh” or “.ksh” or “.bash” suffix on a shell script file name. A program is a program: no benefit exists in distinguishing what gets executed. No unix or linux or whatever kernel has decided to call an interpreter on some file just because of a file name suffix. It’s all done by the #! line, or some other “magic number” sequence of bytes at the beginning of the file. In fact, deciding what to execute based on a file name “extension” is one of the factors that makes Windows a malware magnet. Look at how many Windows malware scams involve a file named “something.jpg.exe” – by default newer Windows don’t show the “.exe” extension, and encourage a user to just double click on the “image”. Instead of an image view running, the malware runs.
What you might think of as a straight-ahead command is often a shell script anyway. Sometimes cc has been a sh-script, firefox is an sh-script, startx is an sh-script. I don’t believe there’s a cognitive or organizational benefit to marking a script with a “.sh” suffix.
Method 3
As one who has worked in a multitude of ?nix environments, I have had to write in a wide variety of shells. Believe it or not, across platforms, the shells are not the same. So if you maintain your personal library in multiple shells (when necessary) it is very helpful to use extensions to ID the shells. That way when you move to another platform and the shell is slightly different, you know what scripts to target for modifications.
.sh .ksh .bsh .csh …
Method 4
You should not use an extension for executables, as they they are not interchangeable. Imagine that you have a shell script a.sh, then re-write in python a.py, you now have to change every program that calls you script, you have leaked implementation detail.
The whole file-name extension thing in Mircosoft’s Windows is a mess: for example what could have been a.audio, b.audio, c.audio, is a.mp3, b.wav, c.ogg, and d.picture, e.picture, f.picture is d.jpeg, e.png, f.gif. Most of the time we do not care what format the audio or picture is in. We also have to spend a long time teaching new users all of the file extensions.
Method 5
As you said it, the Unix file extensions are purely information. You just need your script to have a correct shebang and being executable.
You can either have no extension or using .sh.
I personnaly use the following conventions, regardless of the shell used (csh, tcsh, bash, sh, …):
- no extension for system or high grade scripts (extremely rare).
- the
.shfor classic scripts, low to high grade.
Method 6
Shell script extensions are quite useful. For example I often write scripts that have multiple files in multiple languages (eg. bash, awk and lua) in the same directory. If I need to search for a string in only the bash files, the extension makes this very handy, to reduce false positives. Or if I want to do a line count of all my bash code for that project.
It is a pain to have to type the extension when running the program, so I also make a symlink without the extension to the main executable, to edit/run it without needing to type the extension each time. Symlinks are cheap and easy.
Method 7
As others have said, the shell doesn’t care about extensions. However, it does allow for quick human identification of files. I see files ending in .py or .sh and I quickly know what they (at least) should be. As Steve says, searching by file extension or doing a line count are also practical considerations.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0