-5

I am running some scripts on solaris machine.

Few of them have #!/bin/ksh and #!/bin/sh at the head.

All are working fine? How is that working fine?

Outputs of some commands ...

xxxxx/xx/xxxx/bin(45)#  ps -p $$
   PID TTY         TIME CMD
 17404 pts/3       0:00 ksh

xxxxx/xx/xxxx/bin(46)# echo "$SHELL"
/sbin/sh

xxxxx/xx/xxxx/bin(47)# echo $0
ksh

Thanks, Sagii

Sagii
  • 178
  • 2
  • 13
  • 1
    It's correct for a script written for ksh syntax to request ksh. It's correct for a script written to be compliant with POSIX sh (in very, _very_ new Solaris) or Bourne (in other Solaris systems) to request sh. Is there a reason you think one of them _should_ fail? – Charles Duffy Sep 21 '20 at 04:51
  • 1
    They are like dialect and different people has different preference, or reason to pick one over another. – yosukesabai Sep 21 '20 at 04:51
  • 1
    Stack Overflow questions are expected to be about _a specific problem that you actually face_. Is some program you're trying to write not working, in such a way that an answer would let you correct your program? Note that our sister site [unix.se] is less strict about that rule, and also much more welcoming to questions about UNIX tools that aren't specific to software development. – Charles Duffy Sep 21 '20 at 04:54
  • Only ksh process is running .... sh is not running when i run ps command xxxxx/bin(48)# ps PID TTY TIME CMD 17404 pts/3 0:00 ksh 20961 pts/3 0:00 ps – Sagii Sep 21 '20 at 04:54
  • 1
    I still don't see you describing an actual problem. – Charles Duffy Sep 21 '20 at 04:56
  • My question is can ksh and sh both work at the same time or can only one be running at a time – Sagii Sep 21 '20 at 05:05
  • @Sagii : When a software devloper is writing a program, he decides what language to use - it can be Perl, Ruby, ksh, sh or whatever. When you have several programs on your system running, it is very natural that there are in various languages. This is nothing you need to worry. – user1934428 Sep 21 '20 at 05:57
  • From what I read in documents online ... depending on the header at the top e.g. #!/bin/ksh or #!/bin/bash ... a process will be forked with either bash or ksh executable and pass script name as argument to execute the script – Sagii Sep 21 '20 at 06:20
  • One more doubt is if we execute a script with #!/bin/bash written at top is executed with ksh ... will it fail or work normally? If fail, why?(I may sound naïve but I am learning and have some confusions) – Sagii Sep 21 '20 at 06:22
  • 1
    let's say your default shell is `sh`; when you run `ksh ` you are invoking a new (sub) shell to be run under `ksh`; if `` includes a shebang (eg, `#!/bin/bash`) then this causes yet another new (sub) shell to be invoked which will now be running under `bash`; if you were to pull up a list of your process tree you should see `sh (parent of) ksh (parent of) bash`; when `` finishes the `bash` shell is exited, control is returned to the `ksh` shell, which then immediately exits and leaves you at the command prompt (running under the `sh` shell) – markp-fuso Sep 21 '20 at 11:57

1 Answers1

3

In so many words, these are different dialects of the shell programming language.

Briefly, ksh should be used for running scripts which were designed to use that dialect, and optionally scripts which were designed to be run with sh. If you don't know, it should always be a safe option. But normally you use sh to run scripts which were created for this variant of shell script.

sh is the Bourne shell (or in your case, the Solaris implementation of the Bourne shell). More generally, on modern systems, sh would be expected to implement the POSIX shell language specification, though this is hard to guarantee on an unfamiliar platform.

ksh is the Korn shell, which is backwards-compatible with the Bourne shell. It adds a number of extensions to the base language, many of which are now widely copied by other competing shells. Wikipedia has some additional notes on the history and different versions of ksh.

Digression: Other Shells

The main competitor is bash, which has loaned many features from ksh directly, and implemented many others which are more or less inspired by ksh (or csh, or zsh, or other competitors).

Increasingly, zsh is now also gaining momentum.

The ugly ducklings are the C shell csh and its big brother tcsh, which are not Bourne-compatible, and for that reason increasingly marginal. There was a time when there was less competition, and the features of csh were compelling enough that it accumulated a large following for a while, but that was 30 years ago.

Historically, there was also the Thompson shell, which was the standard shell on Unix until it was superseded by the Bourne shell in v7 Unix. The Programmer's Workbench had a backwards-compatible derivation called the Massey shell.

Among modern shells, dash and ash are relatively faithful reimplementations of the POSIX shell specification. There is also pdksh which started life as a public-domain reimplementation of ksh, but now that ksh itself has a fairly lenient license, it is mainly historical (to my understanding).

Compatibility Notes

In general, given a file script, running ksh script should work flawlessly regardless of whether script was written for Bourne / POSIX shell or specifically for ksh. Similarly, Bash and zsh (in compatibility mode), or any other shell which claims to be compatible or backwards-compatible with the Bourne shell should be able to run a Bourne shell script just fine.

The opposite does not hold. You will get syntax errors, undefined behavior, or silently wrong results if you attempt to run a (proper) ksh (or bash, or zsh, or etc) script in sh, or attempt to run a Bash script in Ksh, or vice versa, or etc.

Unfortunately, "Bourne shell" is not entirely well-defined, or rather, a moving target. In modern parlance, usually sh means the latest POSIX standard, but Solaris in particular was for a long time not compliant with the POSIX specification. And of course, the specification is updated every few years, too. And historically, the original v7 Bourne shell was itself a stable but slowly developing benchmark by itself, especially before POSIX compatibility became the norm.

There are two main variants of ksh, called ksh88 and ksh93. Generally speaking, ksh93 can run ksh88 scripts completely transparently, whereas again, the opposite cannot be expected to work reliably.

Important Ksh features

At the time, the Bourne shell had very crude features for interactive use, so the Korn shell's attraction was strong. Its main competitor at the time was the C shell, which had a number of similarities in terms of feature parity, but of course gave up on Bourne-compatible syntax (in favor of what is traditionally called a "C-like" syntax; but I like to say that if that's C-like, then Marilyn Manson is "Marilyn-like"). The Korn shell site itself says ksh has "the best of both worlds", i.e. Bourne syntax and Csh convenience features, crudely speaking.

The hot features in the late 1980s were interactive command-line editing and history, directory history, job control, and various extensions to the shell language's syntax. Perhaps the most prominent feature loaned into Bash from ksh is the array variable type. At the time, command aliases, arithmetic support, and built-in echo and test commands were also touted as useful features, though the Bourne shell has also been updated to include these features (and aliases are inferior to shell functions anyway).

https://tldp.org/LDP/Bash-Beginners-Guide/html/x7369.html has a brief but rather basic comparison of features between the original Bourne shell, Bash, ksh, and csh.

From the #shell Chat Archives ...

  • ken> hey here's Unix (-: (1971)

  • ken> try the shell, it's pretty simple but I think you'll like it

  • ken> and you know, it's a user-space process, so you can replace it if you like

  • Doug McIlroy> how about redirection? (1973)

  • ken> hang on ... we have redirection

  • bwk> with some pride, we bring you PWB (1977)

  • pjp> lots of useful utilities in a convenient package

  • researchers> lol wow

  • Massey> now with an enhanced shell

  • Massey> you're welcome

  • BillJoy> ive been thinking about a new shell for 2BSD (1979)

  • srb> I have been thinking about a new shell for v7

  • BillJoy> behold! csh!

  • srb> I bring you ... sh

  • freak> I like the features of csh, but ...

  • n00b> I like the syntax of the Bourne shell, but ...

  • {?}> srb: the tricks with the C preprocessor freak me out

  • dgk> look: ksh88 (1988)

  • AT&T> let's use that

  • Sun> let's use that

  • rms> but ... it's not free (1989)

  • rms> @BrianFox have a look

  • BrianFox> announcing GNU Bash, the Bourne Again Shell

  • Linus> here's Linux 0.12, the default shell is Bash (1991)

  • the world> let's use that

  • POSIX> new version 1003.2-1992 out now (1992)

  • POSIX> uses ksh88 as the reference for the shell implementation

  • dgk> hold my beer ... (1993)

  • dgk> okay, here's ksh93

  • the world> but ... it's still not free

  • AT&T> there is now this super weird license (2000)

  • the world> mhm ... "decumbered" you say?

  • AT&T> okay, let's try again: ksh93 is now EPL (2005)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • [Ksh advanced features list, etc](https://stackoverflow.com/a/5527176/620097) . Sorry to say, but with the Korn and Fowler leaving AT&T for Google, ksh is becoming more of an orphan project (There is at least one valuable maintainer, that submits fixes for Linux distributions, Sorry, I don't have that info or follow it closely any more). There is another post here on S.O. (maybe it was removed because O.T.) with more details/comments. Good luck to all! – shellter Sep 29 '20 at 03:54