Are dash, ash and sh script 100% compatible?

18,445

Solution 1

The short answer is no, they're not 100% compatible.

But most of the shells are pretty close to the basic, so you would only rarely bump into inconsistencies. In fact, most shells differ not much by added syntax, but by some extra features like tab-completion and similar.

Also, dash is sort of a descendant of ash — or port from BSD to Linux, to be precise. And all of them should be descendants or different implementations of sh. In fact, sh is on most systems just a symlink to bash, dash or something else. What matters is POSIX compliance — and when you write scripts according to the standards, you won't run into problems.

The difference between those shells is in optimizations and performance. They're less feature-rich then bash, but they are fully legitimate shells. Bash is feature-rich for interactive work, but uses more memory, for example.

Solution 2

It should be mentioned that on some systems, notably Debian, /bin/ash is not available:

$ type ash dash
bash: type: ash: not found
dash is /bin/dash
Share:
18,445

Related videos on Youtube

user1115057
Author by

user1115057

Im interested in learning how to do low level stuff like kernel, os, driver.... Thing i need to learn: Programming (i already know how to write in C, C++, but i still need to think like a programmer...).

Updated on September 18, 2022

Comments

  • user1115057
    user1115057 almost 2 years

    I wanted to know whether scripts written for dash, ash, and sh are 100% compatible?

    Are there any added features to dash or ash, or syntax change?

    From what I heard, ash is a direct descendant of sh.

    • Admin
      Admin almost 12 years
      ash is a striped down form of bash
    • Admin
      Admin almost 12 years
      Why wouldn't people just submit patches to bash instead of creating all kinds of shells, that buffles me all the time. If you really want to know, read the manual, e.g man ash
    • Admin
      Admin almost 12 years
      Correct, it's not descendant from bash. It would be more precise to say they're descendant from sh (the original Bourne shell), but that's not fully correct either.
    • Admin
      Admin almost 12 years
      You can use the utility checkbashisms to test that your script is POSIX compliant.
    • Admin
      Admin about 4 years
      @daisy Why not bash everywhere? Because it isn't always the right tool for the job. Moreover, it's GPL. On top of that, it's quite a lot heavier than sh.
  • user1115057
    user1115057 almost 12 years
    But for script usage, they are compatible?
  • user1115057
    user1115057 almost 12 years
    And about the POSIX compliance, even if I follow that standard, if a script is written for Bash, I dont think it will run on any sh or its descendant
  • Zlatko
    Zlatko almost 12 years
    They should be compatible with each other if you're not using some obscure features. But if you use bash script, you might run into more troubles.
  • user1115057
    user1115057 almost 12 years
    never intended to use bash with sh or ash or dash. What are those obscure feature?
  • Joe
    Joe almost 12 years
    I do not think ash is POSIX compliant but do not quote me on that
  • lynxlynxlynx
    lynxlynxlynx almost 12 years
    many of bash's newer features are additions over POSIX, so it is increasingly likely you will write unportable code if you're using them. IIRC even the [[ ]] construct is not in sh. Also beware, that if you're not writting a pure shell script, difference in tools come into play as well (different versions of sed, grep, awk ...).
  • user1115057
    user1115057 almost 12 years
    I know that there will be difference in tool. But before worrying about that, I need to first start to write portable code, then after I can worry about the tool not exactly the same.
  • muru
    muru over 8 years
    They might have busybox ash.
  • Zoran
    Zoran about 4 years
    That said, there are many systems which don't have bash either.
  • Zoran
    Zoran about 4 years
    There are also some extremely rare systems where sh is a symlink to csh, which breaks a lot of stuff.