Ash Vs Bash

[For reference, I’m talking about Ash in Alpine Linux here, which is part of BusyBox.]

I thought I knew the big differences, but it turns out I’ve had false assumptions for years. Ash does support [[ double square brackets ]] and (as best I can tell) all of Bash’s logical trickery inside them. It also supports ${VARIABLE_SUBSTRINGS:5:12}` which was another surprise.

At this stage, the only things I’ve found that Bash can do that Ash can’t are:

  • Arrays, which Bash doesn’t seem to do well anyway
  • Brace expansion, which is awesome but I can live without it.

What else is there? Did Ash used to be more limited? The double square bracket thing really surprised me.

palordrolap,

ash (and its successor dash found on other distros) is a POSIX-y shell rather than a sh clone, so it has all(? most?) of the POSIX feature set, whose syntax may indeed have been 'borrowed' from shells that came later than sh.

Not sure if there's a "parent" from which both ash and bash inherit the syntax or whether bash is the true source, but that doesn't really matter here.

All that said, it's worth checking to see if your system has a command on the PATH called [[. That has been one way that [[ support can be added to a system when the shell itself might not support it. Note that command names don't have to be alphanumeric like functions tend to be in a programming language (or other languages if you consider that the shell can be used for programming too), so [[ is perfectly valid!

danielquinn,
@danielquinn@lemmy.ca avatar

Yup, that looks like exactly what was done in Alpine:


<span style="color:#323232;">$ docker run --rm -it alpine ls -l /usr/bin/[[
</span><span style="color:#323232;">lrwxrwxrwx    1 root     root            12 Sep 28 11:18 /usr/bin/[[ -> /bin/busybox
</span>

So while the Ash itself doesn’t support the [[ extension, this work-around produces the same effect. Nifty.

jntesteves,
@jntesteves@lemmy.world avatar

Although that link exists, that’s not what is being used by default. [[ is a shell builtin in ash/busybox, so that takes precedence.

On Alpine:


<span style="color:#323232;">❯ which [[
</span><span style="color:#323232;">/usr/bin/[[
</span><span style="color:#323232;">
</span><span style="color:#323232;">❯ command -V [[
</span><span style="color:#323232;">[[ is a shell builtin
</span>
danielquinn,
@danielquinn@lemmy.ca avatar

Huh. So the link is unnecessary and Ash supports [[ out of the box? Good to know, thanks!

suprjami,
@suprjami@lemmy.sdf.org avatar

What have you found bad about bash arrays? I have some simple usage of those (in bash) and they work fine.

danielquinn,
@danielquinn@lemmy.ca avatar

As far as I’ve seen, they don’t provide any advantage over a string with spaces, which doesn’t work well either when you’ve got values with spaces:


<span style="color:#323232;">not_what_you_think=( "a b" "c" "d" )
</span><span style="color:#323232;">for sneaky in ${not_what_you_think[@]}; do
</span><span style="color:#323232;">  echo "This is sneaky: ${sneaky}"
</span><span style="color:#323232;">done
</span>

<span style="color:#323232;">This is sneaky: a
</span><span style="color:#323232;">This is sneaky: b
</span><span style="color:#323232;">This is sneaky: c
</span><span style="color:#323232;">This is sneaky: d
</span>
  • All
  • Subscribed
  • Moderated
  • Favorites
  • linux@lemmy.ml
  • localhost
  • All magazines
  • Loading…
    Loading the web debug toolbar…
    Attempt #