LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   different between test.sh and test.ksh (https://www.linuxquestions.org/questions/linux-newbie-8/different-between-test-sh-and-test-ksh-4175427086/)

brian00 09-13-2012 11:18 AM

different between test.sh and test.ksh
 
1. I don't get it why sometime I saw files with extension .sh and .ksh. I do know sh = bourne shell and .ksh is korn shell. I just don't understand the purpose of when to use it.

2. even with the file .sh, within the file, they still start out with
!#ksh
why?

MensaWater 09-13-2012 01:05 PM

In Linux extensions generally have no purpose like they do in DOS/Windows so aren't required. If someone puts an extension on a file they're just trying to let people know what it is without having to open it or run the "file <file>" command on it.

Many folks put .sh on ALL shell scripts (Bourne, BASH, Korn, Posix sh) and .pl on all Perl scripts. It is not required and neither helps nor hurts the way the script runs. It is more of a convention than anything else. If the contents/ownership/permissions of the following are all the same they would do the same thing:
script.sh
script.ksh
script.bash
script.pl
script.billybob
script
yomama
mymama
this_aint_a_script

Notice in above that I say even if it has a .pl it will run even if the interpreter inside it bash. Since the .pl is just part of the name it doesn't really mean "this is perl". Even naming it "this_aint_a_script" won't keep it from running if in fact it IS a script.

One caveat: They'll all do the same thing so long as there is nothing inside the script that is dependent on the name it was run as. It is actually possible to make one script (or binary) and put code inside it that looks at the name it was called as to determine how to run. That is more commonly seen in binaries than scripts though but I mention it just to avoid the inevitable pedantic response by someone else.

porphyry5 09-13-2012 01:06 PM

Quote:

Originally Posted by brian00 (Post 4779538)
1. I don't get it why sometime I saw files with extension .sh and .ksh. I do know sh = bourne shell and .ksh is korn shell. I just don't understand the purpose of when to use it.

2. even with the file .sh, within the file, they still start out with
!#ksh
why?

The file suffix is just a convention, could be anything. The shebang line determines which shell is used to run the script so all yours are using korn shell. My scripts all have the shebang line
Code:

#!/bin/bash
because I use bash to run them, but I always use '.sh' as the file suffix, not '.bash'

MensaWater 09-13-2012 01:17 PM

Quote:

Originally Posted by brian00 (Post 4779538)
even with the file .sh, within the file, they still start out with
!#ksh

By the way - !#ksh assumes it can find ksh in your $PATH. It is more usual to put the full path to the shell on the interpreter line. (e.g. !#/bin/ksh).

Also the interpreter line itself is not even required. If you run a script in your current shell and it is designed to work in your current shell it will run just fine. However, it is always best to put the interpreter just to insure it runs in the shell you intended it for. Not having the interpreter could be a problem if you were say running in csh then tried to run either a perl or a bash script - csh wouldn't understand the commands it was seeing. The interpreter line tells it to invoke the appropriate shell as to run the commands.

chrism01 09-13-2012 06:16 PM

Quote:

However, it is always best to put the interpreter just to insure it runs in the shell you intended it for.
This++ on prod systems!


All times are GMT -5. The time now is 10:21 AM.