Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 good practices for file extensions for unix shell scripts?
  • Is it common for shell scripts to end with .sh regardless of which shell they run on?
  • Are there any other commonly used file extensions for unix shell scripts?
share|improve this question
3  
Sometimes, shell scripts without shebang (or without exec permissions) can be found. In that case, a name ending in .sh can be a hint to the user to run them with bash script.sh (or sh, of course). –  Ansgar Esztermann Feb 15 '12 at 16:15
1  
If a shell script has an extension it is commonly .sh. I have never seen a .ksh or .bash script. Most shell scripts have no extension though, like all the scripts in /etc/init.d/* as an example. –  Richard Holloway Feb 15 '12 at 22:48
add comment

3 Answers

up vote 6 down vote accepted

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

share|improve this answer
add comment

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 that 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.

share|improve this answer
3  
I disagree! My job consists of packaging an application involving thousands of files ranging from binary executables to shell scripts (ksh, bash and some legacy csh). To me, believe me it does make a difference to be able to know in a glance (or in a regex) what kind of file we are discussing and we are looking for. My point is that there could be a benefit in distinguishing what gets excuted and a best practice should encourage stating explicitly the type of file. –  rahmu Feb 15 '12 at 16:31
2  
@rahmu: write that up as an answer. Give some specifics about how regex-distinguishable names help you package (and maybe maintain) that application. Note specifically the interaction between what interprets the file and the file name's suffix and how that aids you in doing tasks. I'm interested in serious arguments against my viewpoint, and I'm willing to change if I'm convinced. I upvoted your comment to prove it. –  Bruce Ediger Feb 15 '12 at 16:57
2  
I would love to, unfortunately I can only speak of my current experience in my current job. I don't know much about good practice and standards in general; I feel I should do some research before posting an answer here. I'll look into it tonight after work :) –  rahmu Feb 15 '12 at 17:19
1  
@rahmu The "file" command exists for determining file type. It is capable of distinguishing scripts written for different shells. –  Matt Jun 18 '13 at 13:21
add comment

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 .sh for classic scripts, low to high grade.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.