Remix.run Logo
kjellsbells 3 days ago

(I'm sure this is lovely Bash, but for all the people who rejected Perl for its modem line noise vibe...what do ya think of this?)

As an aside, I actually wonder if Bash's caller() was inspired by Perl's.

There is also Carp and friends, plus Data::Dumper when you not only need the stack trace but also the state of objects and data structures. Which is something that I don't think Bash can really do at all.

Grimeton 3 days ago | parent | next [-]

There are no objects in bash. There are indexed and associative arrays and both can be iterated over like so:

  for value in "${SOMEARRAY[@]}"; do
    echo "${value}"
  done
or with the help of the keys:

  for key in "${!SOMEARRAY[@]}"; do
    echo "key: ${key} - value: ${SOMEARRAY["${key}"]}"
  done
If you want to dump the data of any variable you can just use declare -p

  declare -p SOMEARRAY
and you get something like this:

  declare -a SOMEARRAY=([0]="a" [1]="b" [2]="c" [3]="d" [4]="e" [5]="f")
What you can do, if you have a set of variables and you want them to be "dumped", is this:

Let's "dump" all variables that start with "BASH":

  for k in "${!BASH@}"; do
    declare -p "${k}"
  done
Or one could do something like this:

  for k in "${!BASH@}"; do
    echo "${k}: ${!k}"
  done

But the declare option is much more reliable as you don't have to test for the variable's type.
westurner 3 days ago | parent [-]

Bash has associative arrays, but just POSIX shells like ash do not have associative arrays.

And, POSIX shell can only shift and unshift on the $@ array; so it would be necessary to implement hashmaps or associative arrays with shell string methods and/or eval.

westurner 3 days ago | parent | prev | next [-]

Are you asking me to defend shell script syntax, or are you criticizing this except from a shell script?

The awk and printf are as obscure and unreadable as Perl, but still probably faster than just starting Perl.

Ironically, in terms of portability, it's probably more likely that awk and printf are installed than Python (or Perl). This application doesn't need Python in the (devcontainer) container, and nobody does sysadmin scripts with lua (which can't `export VARNAME` for outer shells) so shell scripting is justified though indeed arcane.

Getopt is hardly more understandable than a few loops through $@ with case statements.

I don't understand the relevance of other tools to "getting decent error reports in Bash"?

There are great logging (and TAP testing) libraries in Python, but that doesn't solve for debugging Bash?

There is at least one debugger for Bash scripts.

fragmede 3 days ago | parent | prev [-]

Perl had numerous magic, one/two letter variables and copious regexp use, which is what gave it the line noise insult. Wtf is $@ and $_? After spending enough time breathing Perl you got a feel and could actually read it, but take a break and forget all the things, and it's back to being inscrutable. Meanwhile, the bash above isn't totally inscrutable. It's not python clear, and I don't know what SHLVL is without looking it up, but the rest is clear enough (awk with NR, isn't self explanatory, but unfortunately for me I've used too much awk).