Re: Linux Bash vs. Unix Bash

"Brian K. White" <[email protected]>
Newsgroups gmane.linux.redhat.release.enigma
Organization Aljex Software
Message-ID <003101c30f55$43fac2d0$0e00000a@briandesk>
Scott Queen wrote:
> All,
> Is there a signficant difference between bash in Unix and bash in
> Linux?  (issue driving question: can I study a UNIX bash programming
> book and expect it all to work in Linux?)
>
> Follow on question (this one may not belong here....): What are the
> differences between Bash shells and KornShells?

bash is the same bash wherever it is

however there are significant differences between "sh" on various unii.

"sh" ie: /bin/sh on sco open server for instance is a bourne shell that is
more compatible with ksh than bash, but less fancy than either. a typical
bash script would fail when run in plain sh on sco unix.

converseley, a typical sh script written for sco unix will fail when run
on linux, generally in less critical ways, mostly cosmetic but not
necessarily only cosmetically,

also do not confuse plain "sh" on linux, which is in fact, bash, with
plain sh on sco or any other unix, which are not bash.

ksh is another issue. generally, a typical script that is written for ksh
will look very similar to sh and bash scripts, but it will usually work on
any unix that has ksh, this even includes linux & freebsd where it's more
common to find the gnu free re-implimentation called "pdksh" (public
domain ksh) instead of the real korn shell. the real korn shell is freely
available, but because it's not legal to redistribute binaries and you
must grab the source yourself and build your own binary, it rarely happens
on linux boxes where the typical linux user doesn't even bother installing
pdksh because they don't see the value in writing scripts that will run on
any platform...

plain "sh" on most unixes (besides linux) is usually a subset of features
that both bash and ksh has. they are like 92% overlap in most syntax, but
there are differences.

largely, if you buy a "unix" shell scripting book it should cover all
three main types of bourne/korn  shells because they are so similar and it
they are all about equally present in the world and it's a simple matter
of saying things like:

---snip imaginary text---
example:
  # make a beep
  echo "beep!\a"

note: on linux or in bash on other platforms, the echo command needs "-e"
added in order for backslash codes like "\a" to be interpreted.
---snip------------------

If you have to choose a book that only covered a particular flavor, and
you care about simplicity and compatibility, I'd say get one that covers
ksh. Really though, most unixe's "sh", real ksh, pdksh, and bash, and few
less common other besides, like ash and zsh, are all very very similar in
their basic usage, and mostly only differ in their more advanced features.

the possible differences between the "sh" on any given unix is really just
something you, as a shell script writer, just have to be aware of and
avoid writing things that you know are not likely to be widely compatible
as much as you can. I think of this in terms of trying to stay within the
least common denominator whenever possible, so that things have a greater
chance of "just working" when you copy them to other machines.

examples:

this syntax is good in bash and ksh

n="1"
until [ "$n" -gt "254" ]
do
  echo "10.0.0.$n mylan$n mylan$n.mydomain.com" >>/etc/hosts
  n=$((n+1))
done

however, the $((n+1)) is only available in the more advanced bash and ksh.
there is an equivalent that is slower and less efficient, but would work
in any bourne or korn style shell: `expr $n + 1`

resulting in:

n="1"
until [ "$n" -gt "254" ]
do
  echo "10.0.0.$n mylan$n mylan$n.mydomain.com" >>/etc/hosts
  n=`expr $n + 1`
done

it's slower and less efficient because it fires up a whole 'nuther
instance of sh just to run one expr command and then closes it over and
over again, once for each time the loop runs (254 times in this case)

there are yet other kinds of shells such as csh/tcsh that have practically
nothing in common with sh/bash/ksh and my comments about the different
shells being largely similar do not include things like that, just any
shell that calls itself "sh" or "bourne" or "korn"


Brian K. White  --  [email protected]  --  http://www.aljex.com/bkw/
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro BBx  Linux SCO  Prosper/FACTS AutoCAD  #callahans Satriani
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.