dash as /bin/sh, and now ld –as-needed. Pattern?

November 6th, 2011 by lucas

I must admit that I’ve never been a big fan of the dash as /bin/sh change. I have three main problems with the switch:

POSIX compliance as an argument

Complying to standards is a really good thing. But when everybody is ignoring the standard because they want the comfort of newer features, maybe it’s a sign that the standard should be updated to include those newer features. Most of the bashims used everywhere in scripts were signifiant improvements, like the ability to write:
cp {short1/path1,short2/path2}/very/long/common/path/to/a/file
instead of:
cp short1/path1/very/long/common/path/to/a/file short2/path2/very/long/common/path/to/a/file

The option to improve bash was not fully explored

We started with the premise that bash is bloated, slow, and cannot be improved. Maybe you can help me with that, but I could only find a few simplistic benchmarks comparing dash and bash, and I could not find any analysis of why bash is slow, and why it cannot be improved.
One of the obvious problems is that bash is also an interactive shell, and is linked to ncurses and terminfo, which increases the startup time. But have we investigated the possibility to have a /bin/bash-non-interactive binary that would not be linked to ncurses?

The change was brought to users

While it is OK for Debian (or Ubuntu, in that case, since that change was done in Ubuntu first) to force its developers to use POSIX-compliant scripts, the switch could have been made only to Debian-created scripts (by switching them from a /bin/sh shebang to a /bin/dash shebang, for example). I have trouble justifying that this change was forced on users as well.

Next: linker changes

… and we are doing it again. A set of linker changes (see also the Ubuntu page) was already done in Ubuntu, and is very likely to be done in Debian as well. This switch requires deep changes in some buildsystems (it requires ordering of libraries and forbids indirect dependencies), and is rather painful (it was reverted before the Ubuntu 11.04 release because it was not possible to fix all the packages during the natty release cycle, but is done in the 11.10 release). Of course, there are justifications for this change. But I’m not sure that it’s worth all the trouble created for users.

18 Responses to “dash as /bin/sh, and now ld –as-needed. Pattern?”

  1. gebi wrote on 11/6/11 at 12:55 pm :

    The interesting thing is if taking a look at the bigger picture the only shell which is really there on _every_ plattform is bash, not dash (not only linux, but solaris, *bsd, aix, …)
    So in the end every bashism report on my software is simply fixed by using #!/bin/bash.

    Another point not mentioned is, that IMHO with all those new dash scripts it got SLOWER, not faster because with dash many tasks require you to pipe text through awk/sed/grep/… instead of just doing it in process.
    BUT with systemd all those shellscript foo is hopely going to die a slow death…

    Imho dash scripts really only make sense if scripts that are also needed in initramfs.

  2. Anonymous wrote on 11/6/11 at 12:58 pm :

    Using dash as /bin/sh does improve performance, but that only really matters for boot time; any other time the performance of shell scripts rarely represents the bottleneck.

    So, personally, I’d rather see Debian switch to systemd, kill off the use of shell scripts at boot time, and then use bash as /bin/sh again. :)

  3. Chow Loong Jin wrote on 11/6/11 at 1:02 pm :

    While I’ll agree that alternatives to rewriting a whole shell from scratch should have been explored better, the whole #!/bin/sh business means that you should have been writing POSIX-compliant scripts from the start, and this isn’t just about Dash. I’ve written a number of #!/bin/sh scripts on an OpenEmbedded based system that uses busybox’s ash as its default shell, and could not use many of the Bash extensions.

    In a nutshell, if you want Bash, use Bash. Don’t pretend that you want something else, under the pretext that “it works on most systems anyway.”

  4. edogawaconan wrote on 11/6/11 at 6:45 pm :

    The interesting thing is if taking a look at the bigger picture the only shell which is really there on _every_ plattform is bash, not dash (not only linux, but solaris, *bsd, aix, …)
    So in the end every bashism report on my software is simply fixed by using #!/bin/bash.

    Nope, none of *bsd has bash by default. Even if it’s installed, it won’t be in /bin but somewhere else (usually either /usr/pkg/bin or /usr/local/bin).

  5. Antonio wrote on 11/6/11 at 8:15 pm :

    I’ve adjusted to the POSIX shell since many years now: the only thing I’m still missing is arrays, and even they can be approximated by an eval-fest.

    To be honest though when it starts feeling like an {eval,fork}-fest, it’s usually time to switch to a “real” scripting language.

    As the previous posters said, “#! /bin/bash” works fine if you can’t be bothered to adjust. You’d better make sure you don’t use e.g. Bash 4 features when one of your target platforms only has Bash < 4 preinstalled.

    As for the {str1,str2} expansion, it’s more of a gimmick, and usually indicates you’re hardcoding too much stuff in your script.

    Case in point:

    $ echo $BASH_VERSION
    4.2.10(1)-release
    $ a=1
    $ b=2
    $ echo c{$a,$b}
    c1 c2
    $ echo {$a,$b}c # Parser fail

    $ echo {${a},${b}}c
    1c 2c

    PS: Please enable comment previews!

  6. Jonathan wrote on 11/6/11 at 11:57 pm :

    It would be possible to build Debian packages with –as-needed without changing the default behavior of gcc by putting -Wl,–as-needed in $(dpkg-buildflags –get LDFLAGS).

  7. Jack Russells wrote on 11/8/11 at 2:12 am :

    Interesting blog entry.
    In my opinion, most of your points can be explained by “The CADT Model”, a term coined by JWZ.

  8. oliver wrote on 11/8/11 at 5:47 pm :

    IIUC the linker change has been implemented in RHEL6 as well and is indeed quite annoying (possibly requiring conceptual changes for build systems and releases). And I think the new behavior is already the default on Solaris 10 (and was quite annoying there too). So maybe the necessary build system fixes will be made anyway, and it won’t be that bad for Debian and Ubuntu.

    Btw. is “CADT” this week’s meme? That’s now the second time today I see this term (this accusation) in a blog comment…

  9. Dierdre Crest wrote on 12/2/11 at 8:05 pm :

    Hi, have you ever before asked yourself to write about Nintendo or PSP?

  10. Jayson Palagi wrote on 12/5/11 at 9:41 pm :

    OMG!!! i think he read this blog last nite
    he really loves that shirt
    as rain said before he wants to bring her GF in the army
    he cant bring her but he bringing her name on him

  11. Isaac Justin wrote on 12/28/11 at 2:17 am :

    Just wished to submit and enquire where you acquired your theme? I’m scouting for one for my current weblog and really like yours. Thanks so much.

  12. Eufemia Pale wrote on 01/3/12 at 5:42 pm :

    Thank you for sharing superb informations. Your web-site is very cool. I’m impressed by the details that you have on this web site. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for more articles. You, my friend, ROCK! I found just the info I already searched everywhere and just could not come across. What a great web-site.

  13. Facebooksmiles.Facebook-Smiles.com wrote on 01/7/12 at 1:16 pm :

    Delightfully ..

  14. Judi Curnutt wrote on 01/14/12 at 8:58 am :

    As a Newbie, I am always browsing online for articles that can benefit me. Thank youfirefall gold

  15. Wally Siegfried wrote on 01/23/12 at 4:53 pm :

    I got what you mean , thankyou for putting up.Woh I am pleased to find this website through google. “Money is the most egalitarian force in society. It confers power on whoever holds it.” by Roger Starr.

  16. Benito Sarafian wrote on 02/8/12 at 10:09 pm :

    Hi there, I found your website via Google even as looking for a comparable matter, your site came up, it seems to be good. I have bookmarked it in my google bookmarks.

  17. Shon Lion wrote on 02/15/12 at 10:27 am :

    Thank you a tremendous post, would read your particular others content. thanks for your thoughts for this, I felt a lttle bit strike by this article. Thanks again! You wanna make a great aspect. Has fantastic report here. I feel if a greater number of people consideration for it that way, they’d have a better time have the hold ofing the issue.

  18. Emile Creed wrote on 02/15/12 at 3:09 pm :

    Do you mind if I quote a few of your posts as long as I provide credit and sources back to your weblog? My website is in the very same area of interest as yours and my visitors would genuinely benefit from some of the information you present here. Please let me know if this alright with you. Thanks!