Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Linux Software

A Real Bourne Shell for Linux? 388

the_code_poet asks: "I'm a lead developer for a software development company, and one of my responsibilities has been writing an installer for our product (of which Linux is one of the platforms). In keeping with UNIX tradition, the installer is written in shell (thats /bin/sh), but as many of you know there is no Bourne shell for Linux - only bash. This has caused inconsistencies (mostly bugs in bash) when writing a generic UNIX sh script that works fine on commerical *NIX's." For a semi-complete list of differences between bash and sh, you will want to check out section C1 of the Bourne Again Shell FAQ. To be honest, I have yet to run into much trouble with a script starting with #!/bin/sh with /bin/bash, and I've been using the latter for years. If any of you have had problems related to this, please tell us what the problem was and how you solved it. Also: would anyone out there be interested in writing a real Bourne Shell for Linux?

"On every distro I've ever seen /bin/sh is just a soft link to /bin/bash. If bash is invoked with sh as its name (argv[0]) then its supposed to act like Bourne - but that just doesnt happen (for example: export FOO=bar is *not* valid Bourne shell syntax, you must say FOO=bar; export FOO)

Do you think that the startup scripts for most distributions would break because, even though they say
#!/bin/sh at the top, they REALLY mean #!/bin/bash?

Given that there is no real Bourne shell for Linux, and that bash has an exhorbitant file size. Quoting bash's man page, here: '...it's too big and too slow' for something that is to be used as the defacto-standard shell for scripting, do you think its a worthy venture to set out to write a small, tight, pure Bourne shell?

*asbestos disclaimer*: This has nothing to with Bash as an interactive user shell and has nothing to do with a holy war over who's favorite shell is better than whomever's."

While doing a small bit of research on this question, I noted there was another Bourn-compatible shell out there called "ash", yet it's billed as doing "some things better and some things worse than bash". Does anyone use it, and find it better than bash for their shell scripting needs?

This discussion has been archived. No new comments can be posted.

A Real Bourne Shell for Linux?

Comments Filter:
  • Easy. (Score:5, Informative)

    by Flarners ( 458839 ) on Saturday November 17, 2001 @07:38PM (#2579683) Journal
    1. Download FreeBSD source.
    2. cd /usr/src/bin/sh; make; make install
    AFAIK the BSD Bourne shell is more or less the same as the real Bourne shell.
  • Try this out! (Score:1, Informative)

    by KGB Kenny ( 238697 ) on Saturday November 17, 2001 @07:40PM (#2579690) Homepage
    Why couldn't you just write a small script that chooses what shell to use at the beginning?

    Say the following:

    File 1: Shell script to determine what *nix you are running

    File 2: Bourne shell script

    File 3: bash shell script

    File 1 determines whether to use Bourne shell script of bash shel script and passes it onto decided shell.

    Just a thought... hope I could be helpful!

    -theKGB 8)
  • by dangermouse ( 2242 ) on Saturday November 17, 2001 @07:44PM (#2579708) Homepage
    Slackware uses ash because (a) it's tiny and (b) it's restrictively Bourne-compatible, which means you won't write some tool that is accidentally dependant on bash.

    Basically, Slackware uses ash for development for the exact reasons you're looking for a Bourne-compatible shell.

    Have fun.

  • bash as /bin/sh (Score:4, Informative)

    by zdzichu ( 100333 ) on Saturday November 17, 2001 @07:45PM (#2579710) Homepage Journal
    "On every distro I've ever seen /bin/sh is just a soft link to /bin/bash."
    You haven't seen Polished Linux Distribution [pld.org.pl]. PLD's /bin/sh is not bash, and all 'bashisms' are removed from distribution's scripts - it's all plain /bin/sh.

    (Plus, PLD is fully IPV6 ready :)
  • by psamuels ( 64397 ) on Saturday November 17, 2001 @07:45PM (#2579711) Homepage
    Do you think that the startup scripts for most distributions would break because, even though they say #!/bin/sh at the top, they REALLY mean #!/bin/bash?

    Many would. However, note that POSIX requires /bin/sh to be a POSIX shell, whose specs are derived mostly from the Korn Shell - so ksh is a valid POSIX shell, as is Bash, modulo a few features. Plain-vanilla Bourne shell is not.

    AIX has the same dilemma: to be POSIX-compliant they have /bin/sh -> ksh and /bin/bsh is the real Bourne. HP-UX 11 has /bin/sh distinct from ksh, but it too allows things like 'export FOO=BAR'. I don't know if a real Bourne shell exists on HP-UX.

    For a POSIX shell without the bash overhead, use ash, which is distributed with many (most?) Linux distributions. ash is the NetBSD /bin/sh, and at least on Debian the installation gives you the /bin/sh symlink option as well. Let me tell you, ash is much faster than bash in the autoconf-generated-configure "benchmark".

    Since many Debian people use ash for /bin/sh, packages regularly have bugs filed -- and fixed -- vis-a-vis #!/bin/sh vs. #!/bin/bash. I don't know how careful other distros are about this sort of thing. Note that even many Debian scripts would fail if you found a real Bourne shell for /bin/sh rather than a POSIX-compliant shell.

  • by Anomie-ous Cow-ard ( 18944 ) on Saturday November 17, 2001 @07:47PM (#2579719)
    Do you think that the startup scripts for most distributions would break because, even though they say #!/bin/sh at the top, they REALLY mean #!/bin/bash?

    I couldn't say for other distros, but Debian policy [debian.org] says that /bin/sh can be any POSIX-compatible shell, with the one extension that 'echo -n' must not generate a newline. If any script uses /bin/sh assuming bash features, it's considered a serious-level bug.

  • Re:It's a non-issue. (Score:5, Informative)

    by pete-classic ( 75983 ) <hutnick@gmail.com> on Saturday November 17, 2001 @08:09PM (#2579781) Homepage Journal
    What you are saying is valid, but it misses the point.

    Even if every valid bourne script runs perfectly under bash there is still a problem.

    That problem is that an interpreter enforces valid syntax. It would be nice if everyone who wanted to write a portable (read bourne) script spent a month studying and was totally zen-ed out on the subject. But that 'aint the real world.

    I personally could benefit from a real sh on Linux, since it is the only *NIX I have, and I can't test scripts for sh compatibility without it.

    To be more specific, I wrote a bash script that was included in an OSS project. The maintainer wanted me to change the first line from "#!/bin/bash" to "#!/bin/sh" "for compatibility." I told him that I wasn't comfortable with this since the script wasn't tested on sh. He said he tested it and it worked fine. I asked him if he really tested it with sh or with "/bin/sh -> /bin/bash". Turned out it was the latter. Apparently he made the change anyway.

    A while later we got a bug report on the list to the effect "your script is not a valid sh script." Luckily the guy sent a diff, and I tested it with bash. I guess it works with sh now too, but I still don't know for sure.

    See how it isn't a non-issue, at least for me?

    -Peter
  • Re:2nd hit on Google (Score:2, Informative)

    by Vanbo ( 75942 ) on Saturday November 17, 2001 @08:25PM (#2579821) Homepage
    Ok guess its never going to get modded up because it has FreeBSD in it...

    Here is the source to what is very close to "the real thing"

    ftp://ftp.freebsd.org/pub/FreeBSD/branches/4.0-s ta ble/src/bin/sh/

    So the answer is still, "Use the real bourne sh" and here is how you do it. Download, compile (on linux) and install to some place that won't effect linux. Have your install script call the newly made "real" shell.

    Wasn't hard eh?
  • by cvanaver ( 247568 ) on Saturday November 17, 2001 @09:15PM (#2579927)
    I've run into this problem in the past as well. My solution was to abandon Bourne shell as a scripting shell and use ksh instead. To be 100% portable, you should use Bourne, but most of your major *NIXes have support for ksh (including linux, Solaris and AIX..not sure about HP-UX) and it has a lot of nice scripting gizmos like associative arrays, printf command, co-processes, pattern matching, etc. Of course, maybe you don't need all of that stuff.

    As an aside, there has been a lot of extensive research on making portable shell scripts. Bruce Blinn's book Portable Shell Programming: An Extensive Collection of Bourne Shell Examples [amazon.com] is a good resource that might help as well as GNU Autotools documentation [huji.ac.il], which is the definitive guide on this sort of thing. Another useful jumping off point that has some good materials and a lot of useful links is Shell Script Porting Guidelines [raycosoft.com]
  • by psamuels ( 64397 ) on Saturday November 17, 2001 @09:55PM (#2579997) Homepage
    I like csh a lot better because it's easier to configure, easier to use, and has a syntax that I like a lot better.

    This reads like a troll, but it could be sincere, so I'll treat it seriously. How do you open more than two output file or more than one input file concurrently? csh has no primitives for opening and closing files beyond redirecting a single command or pipeline. Even for something as simple as redirecting stdout to one file and stderr to another, you have to jump through hoops.

    I won't read back to you the famous Considered Harmful [perl.com] file, but the above point is probably the main reason I would never program with csh scripts. Sure, csh has some programming features Bourne shell lacks, but ksh/bash/POSIX have those same features. Opening and closing arbitrary files is something I do in my scripts all the time.

  • by Anonymous Coward on Saturday November 17, 2001 @09:56PM (#2580002)
    >> Re:Did you even read the complaint?
    > Yes. I did.
    > And it was nonsensical.

    It just appeared that way because you didn't understand it. Read it again and concentreate :-).

    The poster wasn't complaining of POSIX things not working in bash, he was complaining of non-POSIX things working.

    If you don't understand why this is important, you shouldn't be posting in this thread: Good software engineering requires the ability to QA software in an environment similar to that of your target platform. If your target platform has bourne shell only, and your development environment is linux, where the closest thing to sh is a bit different, then you're missing an element that's usually required to produce good software.

    Ironically, you yourself prove this point by complaining about the problems that come up when /bin/sh is relinked to ash because many scripts on your system have bash dependencies, in spite of their authors targeting them for sh (as indicated by the fact that they put #!/bin/sh in their headers).

    IMHO the solution to this real problem is to submit bugs to bash for any behavior that's incompatible with plain bourne when it's invoked as /bin/sh. Fixing bash should be simpler than writing a new bourne shell from scratch. Fixing all the scripts that depend on bash being broken, of course, would be another matter entirely, but these scripts could simply change to #!/bin/bash in their headers to confess to their bashness.
  • Re:It's a non-issue. (Score:2, Informative)

    by psaltes ( 9811 ) on Saturday November 17, 2001 @09:57PM (#2580003)
    > What you are saying is valid, but it misses the point.

    Actually, what he says is not even valid. If anyone had bothered to read the link that Cliff helpfully posted, they would see that there are several things bash does not do, and several things that are 'implemented differently'. The following is taken from that page:

    Things sh has that bash does not:
    uses variable SHACCT to do shell accounting
    includes `stop' builtin (bash can use alias stop='kill -s STOP')
    `newgrp' builtin
    turns on job control if called as `jsh'
    $TIMEOUT (like bash $TMOUT)
    `^' is a synonym for `|'
    new SVR4.2 sh builtins: mldmode, priv

    Implementation differences:
    redirection to/from compound commands causes sh to create a subshell
    bash does not allow unbalanced quotes; sh silently inserts them at EOF
    bash does not mess with signal 11
    sh sets (euid, egid) to (uid, gid) if -p not supplied and uid 100
    bash splits only the results of expansions on IFS, using POSIX.2
    field splitting rules; sh splits all words on IFS
    sh does not allow MAILCHECK to be unset (?)
    sh does not allow traps on SIGALRM or SIGCHLD
    bash allows multiple option arguments when invoked (e.g. -x -v);
    sh allows only a single option argument (`sh -x -v' attempts
    to open a file named `-v', and, on SunOS 4.1.4, dumps core.
    On Solaris 2.4 and earlier versions, sh goes into an infinite
    loop.)
    sh exits a script if any builtin fails; bash exits only if one of
    the POSIX.2 `special' builtins fails
  • by Anonymous Coward on Saturday November 17, 2001 @10:09PM (#2580025)
    > I think that there is some confusion here.

    Indeed there is. In fact, you seem quite confused :-)

    > Things that you can write under bash that
    > won't work under sh. So what?

    So what? Some people like to test their software. I know, it's an old fashioned idea, but let's try to humor those quaint folks in this thread.

    Having things work under bash that won't work under sh is exactly what makes it hard to test sh scripts in an environment that only has bash.

    Moderators, you might want to mark down messages like the parent that miss the point entirely.

    Now back to the point, which is discussing how we can write scripts to be run on environments with bourne shell only when we're developing on linux, which doesn't have a fully compatible bourne shell.
  • Linux Standard Base (Score:5, Informative)

    by kingdon ( 220100 ) on Saturday November 17, 2001 @10:38PM (#2580072) Homepage

    Others have addressed the various issues about what is a "real" Bourne shell and bash extensions and all that. Anyway, the Linux Standard Base has a section [linuxbase.org] on shells. In a nutshell, bash 2.x was the most POSIX-compliant of the shells that the LSB tested (and no, I don't know exactly what versions or which shells or the like), with pdksh getting an honorable mention. And there were two ways in which bash was not POSIX-compliant and concerning which the LSB therefore diverges from POSIX (whether $0 is the full pathname or just the basename, and what happens if you try to use "." to run a script without the read bit set). I don't know whether a future version of POSIX is planning to change the specification, or whether this is likely to remain a divergence for the foreseeable future or what. In any event, these two issues shouldn't be hard to deal with in writing scripts.

  • Re:Easy. (Score:2, Informative)

    by bwulf ( 325 ) on Saturday November 17, 2001 @11:53PM (#2580214)
    4.4BSD-Lite (and derivatives) /bin/sh is Ash.

    Also, recent FreeBSD 4.x releases have replaced 4.4BSD csh with tcsh (original csh being available as a port).
  • by tbuskey ( 135499 ) on Saturday November 17, 2001 @11:54PM (#2580217) Journal
    csh isn't portable

    I had to port a 1500 line csh script from Ultrix to SunOS, Solaris, Dec OSF, HP-UX, and SGI Irix.

    After getting it to work on Ultrix and SunOS, I tried to get it going on Solaris. csh wasn't even portable from SunOS to Solaris!

    The solution was to use /bin/sh. It is portable. I bet that code would work on bash too.
  • Re:Too much history (Score:2, Informative)

    by taniwha ( 70410 ) on Sunday November 18, 2001 @03:50AM (#2580572) Homepage Journal
    ok - as a one-time algol programmer, and user of Unix V6 (god I'm dating myself :-) - I think Steve had Algol68, not Algol itself on the brain.



    Circa 1971 Algol68 was a 'state of the art launguage', mind you it was one that proved to be a bit too hard to implement well. Most of the easy stuff ended up in Pascal (and some in pascal).



    C's 'struct' and 'union' keywords for example almost surely came from (or via) algol68

  • by AndyElf ( 23331 ) on Sunday November 18, 2001 @05:17AM (#2580653) Homepage
    To quote from FreeBSD Handbook [freebsd.org]:

    6.12. Why is /bin/sh so minimal? Why doesn't FreeBSD use bash or another shell?

    Because POSIX says that there shall be such a shell.

    The more complicated answer: many people need to write shell scripts which will be portable across many systems. That is why POSIX specifies the shell and utility commands in great detail. Most scripts are written in Bourne shell, and because several important programming interfaces ( make(1), system(3), popen(3), and analogues in higher-level scripting languages like Perl and Tcl) are specified to use the Bourne shell to interpret commands. Because the Bourne shell is so often and widely used, it is important for it to be quick to start, be deterministic in its behavior, and have a small memory footprint.

    Granted, not really Linux-related answer, however it does answer the question. Also, note last para of the question -- this is not about interactivityportability.

  • Re:Easy. (Score:0, Informative)

    by Flarners ( 458839 ) on Sunday November 18, 2001 @02:26PM (#2581405) Journal
    They do tell you not to change root's shell, but that has nothing to do with scripting. It is because csh (and everything else in /bin and /sbin on *BSD) is statically linked, whereas bash, zsh, rc or whatever shell you may have installed through the ports tree will be dynamically linked. This is important in case your /usr filesystem goes berserk; if root's shell is /usr/local/bin/bash and /usr isn't available, you're up the proverbial creek.

E = MC ** 2 +- 3db

Working...