28

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.

Mat
  • 51,578
  • 10
  • 158
  • 140
user1115057
  • 795
  • 3
  • 12
  • 17
  • ash is a striped down form of bash – Joe Aug 07 '12 at 06:14
  • 4
    Source? I saw nothing that said that ash is related to bash – user1115057 Aug 07 '12 at 06:25
  • 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` – daisy Aug 07 '12 at 06:36
  • 3
    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. – Zlatko Aug 07 '12 at 06:37
  • 1
    You can use the utility [`checkbashisms`](http://sourceforge.net/projects/checkbaskisms/) to test that your script is POSIX compliant. – donothingsuccessfully Aug 07 '12 at 17:37
  • @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`. – Tripp Kinetics Jun 09 '20 at 18:05

2 Answers2

24

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.

Jonathan Leffler
  • 1,479
  • 13
  • 14
Zlatko
  • 508
  • 4
  • 9
  • But for script usage, they are compatible? – user1115057 Aug 07 '12 at 06:36
  • 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 – user1115057 Aug 07 '12 at 06:37
  • 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. – Zlatko Aug 07 '12 at 06:41
  • never intended to use bash with sh or ash or dash. What are those obscure feature? – user1115057 Aug 07 '12 at 06:42
  • I do not think ash is POSIX compliant but do not quote me on that – Joe Aug 07 '12 at 07:21
  • 1
    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 ...). – lynxlynxlynx Aug 07 '12 at 11:44
  • 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. – user1115057 Aug 07 '12 at 14:47
  • There are also some extremely rare systems where `sh` is a symlink to `csh`, which breaks a lot of stuff. – Tripp Kinetics Jun 09 '20 at 18:24
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
Zombo
  • 1
  • 5
  • 43
  • 62