# simple shell question



## Alain De Vos (Oct 21, 2022)

is /bin/sh more like a Korn-Shell or more like a Bourne-shell or still something else ?
And which one is best ?


----------



## mer (Oct 21, 2022)

Alain De Vos said:


> is /bin/sh more like a Korn-Shell or more like a Bourne-shell or still something else ?
> And which one is best ?


I think it's better to say that "Bourne-shell and Korn-shell are more like /bin/sh".
In the grand scheme of things, to the best of my knowledge /bin/sh is the "Bourne shell", I believe the original shell for BSD/Unix.  Then came csh.  Bash, tcsh, ksh, etc are all after that.


----------



## Alain De Vos (Oct 21, 2022)

Currently my root shell is zsh and it executes all the sh in rc.d scripts just fine.
My recovery shell toor is oksh. As it has no dynamic dependencies.
Still I wonder is it worth to learn ksh ?


----------



## Cthulhux (Oct 21, 2022)

Alain De Vos said:


> is /bin/sh more like a Korn-Shell or more like a Bourne-shell or still something else ?



/bin/sh is the Bourne shell.



Alain De Vos said:


> And which one is best ?



Depends. For interactive use, probably either the tcsh (once configured - _that's my personal configuration, you probably don't need all of it_) or the zsh. For scripting, the KornShell (both _mksh_ and _ksh93_ are pretty good); but then again, I warmly recommend to use Perl or something for scripting, instead of a shell. I, personally, am currently migrating from _tcsh_ to the _rc_ shell because I really don't need many interactive features. To each their own, I guess.



mer said:


> I believe the original shell for BSD/Unix.



csh is the BSD shell. Unix had several shells, the Thompson shell was the second one if I remember correctly; then, the Bourne shell, which was replaced by _rc_ in the last Unix version, "V10 UNIX".


----------



## mer (Oct 21, 2022)

Alain De Vos said:


> Currently my root shell is zsh and it executes all the sh in rc.d scripts just fine.
> My recovery shell toor is oksh. As it has no dynamic dependencies.
> Still I wonder is it worth to learn ksh ?


My opinion:
Shells fall into 2 basic camps.
csh and sh (bourne).  almost all of the current shells are derivatives/extensions of these.
tcsh is an extended csh
bash is an extended sh (hence it's name of Bourne Again sh)
zsh, ksh are like bash, extended versions of sh.
Beyond the compatibility, the extensions are what make scripts break.  Use bashisms for substitutions and try to run it with raw sh?  Gonna break.  Same thing for zsh/ksh/whatever-sh.
I tend to use tcsh for my interactive term sessions, bash/sh for my scripting.
Learn another?  Maybe worth it to do quick overview of differences between a new one and one you know.  Then if you come across a script written in the new one, you can easily adapt.


----------



## Cthulhux (Oct 21, 2022)

mer said:


> csh and sh (bourne). almost all of the current shells are derivatives/extensions of these.



No, not quite - especially newer shells tend to reinvent the wheel.



mer said:


> Use bashisms for substitutions and try to run it with raw sh? Gonna break. Same thing for zsh/ksh/whatever-sh.



Even worse: zsh claims to have "compatibility modes" for other shells. None of those are not broken.


----------



## eternal_noob (Oct 21, 2022)

Alain De Vos said:


> Still I wonder is it worth to learn ksh ?





eternal_noob said:


> Everything is worth trying. (Well, almost everything.)


----------



## Andriy (Oct 21, 2022)

Alain De Vos said:


> is /bin/sh more like a Korn-Shell or more like a Bourne-shell or still something else ?
> And which one is best ?


Yes.
Hope this helps


----------



## Alain De Vos (Oct 21, 2022)

I think readability of shell script code is important. For the moment i go for "mksh" for scripts.


----------



## Cthulhux (Oct 21, 2022)

Alain De Vos said:


> I think readability of shell script code is important.



Most languages are more readable than shell script.


----------



## gpw928 (Oct 21, 2022)

If you care about the Genealogy...

The Bourne Shell (written by Stephen Bourne) was introduced in V7 Unix. It rapidly usurped the V6 shell (written by Ken Thompson).

Some years later, David Korn modified the Bourne Shell to produce the Korn Shell.  Its principal feature was command history and editing.  It took quite a while for that to escape Bell Labs.  I got  a bootleg copy in 1983.

In FreeBSD, I believe that `/bin/sh` is the Almquist Shell -- an unencumbered Posix compliant version of the System V Bourne Shell.


----------



## Alain De Vos (Oct 22, 2022)

I found an interesting new interactive shell 'nushell'. 
But for scripts 'mksh' is not bad.


----------



## Cthulhux (Oct 22, 2022)

Nushell is very incomplete yet, especially on Windows it has a few annoyances left to fix.


----------



## Alain De Vos (Oct 22, 2022)

I don't use Windows on my desktop. But nushell looks interesting to filter,sort data in tables.
Note, bash is better documented ...


----------



## Cthulhux (Oct 22, 2022)

Alain De Vos said:


> But nushell looks interesting to filter,sort data in tables.



It sure does, and it works just fine even with third-party applications. It has matured a lot in the past few months.


----------



## Alain De Vos (Oct 22, 2022)

I tried nushell on freebsd. The only command showing in a table is "ls".
For instance "zfs list" does not show a table, nor does "ps" show a table.
So it's in this sense rather very limited.
But the idea is behind is good.


----------



## chrbr (Oct 25, 2022)

Hello Alain De Vos,
please excuse the late reply. I think https://hyperpolyglot.org/unix-shells is a good comparison of different shells.


----------



## forquare (Oct 25, 2022)

Alain De Vos said:


> Still I wonder is it worth to learn ksh ?


The only people I come across that regularly write ksh scripts is Oracle DBAs - I know the odd old (pre-Oracle) Solaris admin that also uses it for both scripting and interactive use.

Personally my root shell is whatever the default is, my personal interactive shell is ZSH, and as much as possible I script in POSIX compliant sh with POSIX compliant tooling/arguments - depending on the task, I'll make sure it runs on Linux, macOS, and FreeBSD, I use all three so generally like my scripts to be portable.


----------



## gpw928 (Oct 25, 2022)

forquare said:


> The only people I come across that regularly write ksh scripts is Oracle DBAs


On AIX, all system scripting is done in ksh.  It's generally the only shell that AIX people know.


forquare said:


> generally like my scripts to be portable.


Agreed, but shell is a pretty awful programming language. I have always been of the view that as soon as a shell script got complicated it was time to move to perl(1), but that just makes me old fashioned these days.


----------



## Alain De Vos (Oct 25, 2022)

I just found ksh93 knows associative arrays.


----------



## Holger (Oct 25, 2022)

gpw928 said:


> On AIX, all system scripting is done in ksh.  It's generally the only shell that AIX people know.
> 
> Agreed, but shell is a pretty awful programming language. I have always been of the view that as soon as a shell script got complicated it was time to move to perl(1), but that just makes me old fashioned these days.


perl(1) is much bashed upon (no pun intended!) when you google around what could be a proper scripting language. In fact, from my own experience, I find Perl pretty awful. For example: How do you prove that your regex doesn't yield a false positive? Is there support for proper way of testing your code, etc.? In general, I frown upon the whole “bash on stereoids”-pradigm that lies at the heart of Perl. Let alone the horrible ”there are a thousand ways of doing things”-syntax.

But I see it mentioned regularly here in the forum. Which makes me wonder: Is there something I do not (yet) know? Why is Perl the superior scripting languange? What are its benefits?

(sorry for getting off-topic …)


----------



## Alain De Vos (Oct 25, 2022)

I find "lua" not bad as language. It's also used by the freebsd-boot-loader.


----------



## Cthulhux (Oct 26, 2022)

Holger said:


> Why is Perl the superior scripting languange?



It is a mature, portable language that does text processing better than any other language - and it comes preinstalled on most operating systems these days *and* you can be relatively sure that `perl` is Perl 5, not "GNU Perl", not "BSD Perl", not a symlink to Python.



Alain De Vos said:


> I find "lua" not bad as language. It's also used by the freebsd-boot-loader.



Do you plan to turn this thread into "Alain De Vos lists every language he has seen so far"? Just asking for continuity.


----------



## gpw928 (Oct 26, 2022)

Holger said:


> I frown upon the whole “bash on stereoids”-pradigm that lies at the heart of Perl.


Bash was released in 1989.  It is not Posix compliant.
So bash scripts are only portable if you have bash (not usually an issue these days, but it was in the past).
Perl dates back to 1987.  So perl was first.  I remember unpacking it from comp.sources.unix.
Perl has been universally available and portable from its inception.
In fact, perl's ./Configure script paved the way for vast improvements in free software portability.


Holger said:


> I find Perl pretty awful... What are its benefits?


It's certainly not perfect, but I find bash "pretty awful" to a much greater degree (for scripting).  Perl is:

designed from scratch as a scripting language;
mature (with a vast library);
well documented;
portable;
powerful;
has integrated protections against hacking -- vital for CGI scripts -- with taint method; and
really fast at just about everything, but orders of magnitude faster than any shell on string handling.
Speed can be vital if you are processing large text files in real time.

Also, I know it's historical, but perl's pattern matching was just stunning (not to mention trail blazing).

Like C and C++, perl requires professional discipline to produce "good" code.  i.e. there's a vast amount of rope, and plenty enough to hang yourself -- but there's usually little glory without power.


----------



## smithi (Oct 26, 2022)

gpw928 said:


> Perl dates back to 1987.  So perl was first.  I remember unpacking it from comp.sources.unix.
> Perl has been universally available and portable from its inception.
> In fact, perl's ./Configure script paved the way for vast improvements in free software portability.



REXX dates back to '79, though not ANSI standard until '96.  (It's ok to be off-topic in this topic, right?)

https://en.m.wikipedia.org/wiki/Rexx



gpw928 said:


> It's certainly not perfect, but I find bash "pretty awful" to a much greater degree (for scripting).  Perl is:
> 
> designed from scratch as a scripting language;
> mature (with a vast library);
> ...



More or less readable.  Bash code may be readable, but I find almost all GNU docs virtually impenetrable, so I rarely even try.

I used OS/2 REXX from c. '92, later rexx-imc on FreeBSD from maybe '99-'05, the latter for billing our internet-sharing club members from ppp and login logs; superb for text parsing but I can't compare it with perl.  For serious stuff I use FreePascal (weird, eh?)



gpw928 said:


> Speed can be vital if you are processing large text files in real time.
> 
> Also, I know it's historical, but perl's pattern matching was just stunning (not to mention trail blazing).



OS/2 REXX was fast, rexx-imc not so much, but author Ian Collier was then very helpful, as was the disparate rexx community, FidoNet list etc.



gpw928 said:


> Like C and C++, perl requires professional discipline to produce "good" code.  i.e. there's a vast amount of rope, and plenty enough to hang yourself -- but there's usually little glory without power.



Same can be said for any useful language I guess?

I've been working on bits of bsdconfig(8) which is sh on steroids.  Talk about obscurity, though it does make sense eventually, and (mostly) works ...

cheers


----------



## forquare (Oct 26, 2022)

gpw928 said:


> I have always been of the view that as soon as a shell script got complicated it was time to move to perl(1), but that just makes me old fashioned these days.


I've heard lots of people say versions of this, and everyone seems to differ on what exactly "complicated" means.  Some have a seemingly arbitrary line count of maybe 100 LOC, others suggest when you start using functions or external tools inside the script like awk/sed/perl/etc in an "advanced" way...

I mostly agree that at a certain point it's time to move away from shell to _something_, but to what?  I used to know Perl fairly well, but have only ever worked with two other people who have understood it.  I don't enjoy writing Python but has been a bit more common.  I detest javascript, but it's been common in some workplaces.
The most commonly "understood" scripting language I've come across in most of my workplaces is shell (plus various external tools like awk/sed/curl/jq)...Admittedly nothing is ever _very_ complicated, and most people I've come across don't know how to write portable and maintainable shell scripts...

I tried to do some "personal fun stuff" in Perl a coupe of years ago and found that CGI had been removed from core and was generally discouraged in favour of some other frameworks.  After trying to relearn Perl and one of the frameworks I eventually gave up and wrote some Go for the first time which seemed to come a little more naturally to me now 

Sorry, taken this way off topic now!


----------



## kpedersen (Oct 26, 2022)

forquare said:


> I tried to do some "personal fun stuff" in Perl a coupe of years ago and found that CGI had been removed from core and was generally discouraged in favour of some other frameworks.


I notice with Perl and Python it is best to just ignore notices saying that things are deprecated. Usually it is just another developer on the PIP, CPAN trying to push an agenda.

For example the Git http-backend uses Perl CGI.pm and that is likely going to stay around much longer than whatever CGI replacement Perl is using these days.


----------



## SirDice (Oct 26, 2022)

There once was a time when every dynamic website was built around Perl's CGI modules. Perl is awesome but it can be quite unreadable due to its lax syntax rules. I actually learned to code Perl on my Amiga. Then used it a lot professionally on Windows (Remember Activestate Perl?) to make life as an admin a little easier. A few years later I worked for a large international oil company. None of the available commercial network management products were capable of dealing with the large amount of routers and switches they had. So they wrote their own sofware, in Perl. The team I worked for had to manage and maintain that software. If I screwed up there would be around 2000 network engineers world-wide just sitting on their thumbs not being able to do their work.


----------



## Whattteva (Oct 26, 2022)

kpedersen said:


> I notice with Perl and Python it is best to just ignore notices saying that things are deprecated. Usually it is just another developer on the PIP, CPAN trying to push an agenda.
> 
> For example the Git http-backend uses Perl CGI.pm and that is likely going to stay around much longer than whatever CGI replacement Perl is using these days.


I wouldn't say they have a specific agenda. It's really more that they fall into herd mentality of jumping on whatever the new and shiny bandwagon is. I see this across disciplines and industries.
In my world (mobile development), everyone on iOS is going on and on about how Swift is the best bla bla bla and C sucks and you should move away from it because it's old (as if that's an actual reason). Similarly, on the Android side, the same thing is happening except with Java and Kotlin. None of them have any deeply-thought agenda. Most of them are just jumping on the bandwagon just because everyone else said it's the hottest thing since sliced bread.


----------



## Alain De Vos (Oct 26, 2022)

Scala as alternative for Kotlin ?


----------



## forquare (Oct 26, 2022)

SirDice said:


> I actually learned to code Perl on my Amiga.


I thought Perl was very learnable - I learnt it while on night shifts downloading blackbox signalling logs on trains over a slow serial connection, just reading the Camel Book and doing the exercises in my head.
When I eventually sat down at a computer to do something Perl just flowed really nicely, never had that experience with any other language...


----------



## Cthulhux (Oct 26, 2022)

Alain De Vos said:


> Scala as alternative for Kotlin ?



Depends on what you want to do.


----------



## gpw928 (Oct 26, 2022)

forquare said:


> I've heard lots of people say versions of this [use perl when shell gets complicated], and everyone seems to differ on what exactly "complicated" means.


I used Stephen Bourne's original shell a lot.  So I know what I think all its derivatives and look-alikes should have in common.

My rule has always been to use perl as soon as the job can't be done in strict traditional (and very portable) Bourne shell.

For that reason my shell scripts will generally run on sh, ash, dash, bash, zsh, ksh, etc.  That goes for system scripts on Linux because Debian uses dash, while other distributions use bash.

Feeling the need for arrays of any kind is often the first warning.  But for scripting I would generally choose perl over shell if the task required:

lots of string processing;
arrays of any kind;
compound data structures (stacks, trees, linked lists, etc.);
complex pattern matching;
genuine speed; or
CGI in a web server (never any shell).
Speed can be a determining factor.  Most shells are internally much slower than perl (and bash is an utter pig), but shell scripts also often need to run external processes to get things done (e.g. cut, grep, sed, awk, expr, etc.), where perl does not.  Forking and execing one or more process once per loop can get extremely tiresome as the loop count increases.  Processing web logs in real time (so you finish before the next one is due) is the classic example.


----------

