0
()
submitted a long while ago by @ to c/@
all 10 comments
sorted by: hot top controversial new old
[-] suprjami@lemmy.sdf.org 15 points 8 months ago* (last edited 8 months ago)

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

[-] danielquinn@lemmy.ca 8 points 8 months ago

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:

not_what_you_think=( "a b" "c" "d" )
for sneaky in ${not_what_you_think[@]}; do
  echo "This is sneaky: ${sneaky}"
done
This is sneaky: a
This is sneaky: b
This is sneaky: c
This is sneaky: d
[-] shmanio@lemmy.world 21 points 8 months ago* (last edited 3 months ago)

You should put some quotes where you use the array:

not_what_you_think=( "a b" "c" "d" )
for sneaky in "${not_what_you_think[@]}"; do
  echo "This is sneaky: ${sneaky}"
done

This is sneaky: a b
This is sneaky: c
This is sneaky: d
[-] danielquinn@lemmy.ca 4 points 8 months ago

Aaaaah! Thank you kind stranger. It never would have occurred to me to quote an array!

[-] suprjami@lemmy.sdf.org 11 points 8 months ago

If you run your scripts through https://shellcheck.net it'll pick up things like this. Also available as a Linux package for offline use.

[-] zatanas@lemm.ee 6 points 8 months ago

I too would like to know. Thank you.

[-] palordrolap@kbin.social 9 points 8 months ago* (last edited 8 months ago)

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@lemmy.ca 3 points 8 months ago

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

$ docker run --rm -it alpine ls -l /usr/bin/[[
lrwxrwxrwx    1 root     root            12 Sep 28 11:18 /usr/bin/[[ -> /bin/busybox

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

[-] jntesteves@lemmy.world 5 points 8 months ago

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:

❯ which [[
/usr/bin/[[

❯ command -V [[
[[ is a shell builtin
[-] danielquinn@lemmy.ca 1 points 8 months ago

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

this post was submitted on 01 Jan 0001
0 points (NaN% liked)

0 readers
0 users here now

founded a long while ago