LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-13-2012, 11:18 AM   #1
brian00
Member
 
Registered: Aug 2011
Posts: 33

Rep: Reputation: Disabled
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?
 
Old 09-13-2012, 01:05 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 09-13-2012, 01:06 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
Quote:
Originally Posted by brian00 View Post
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'
 
Old 09-13-2012, 01:17 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by brian00 View Post
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.
 
Old 09-13-2012, 06:16 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Load test, boundary test & stress test for USB EHCI/xHCI driver rama_toshiba Linux - Kernel 5 02-29-2012 02:43 PM
[SOLVED] Silencing the line "echo test > test/test.txt" in a shell script Arenlor Linux - General 2 06-18-2010 01:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration