Multidimensional associative arrays in Bash

You can’t do what you’re trying to do: bash arrays are one-dimensional $ declare -A PERSONS $ declare -A PERSON $ PERSON[“FNAME”]=’John’ $ PERSON[“LNAME”]=’Andrew’ $ declare -p PERSON declare -A PERSON='([FNAME]=”John” [LNAME]=”Andrew” )’ $ PERSONS[1]=([FNAME]=”John” [LNAME]=”Andrew” ) bash: PERSONS[1]: cannot assign list to array member You can fake multidimensionality by composing a suitable array index … Read more

How to get bc to handle numbers in scientific (aka exponential) notation?

Unfortunately, bc doesn’t support scientific notation. However, it can be translated into a format that bc can handle, using extended regex as per POSIX in sed: sed -E ‘s/([+-]?[0-9.]+)[eE]\+?(-?)([0-9]+)/(\1*10^\2\3)/g’ <<<“$value” you can replace the “e” (or “e+”, if the exponent is positive) with “*10^”, which bc will promptly understand. This works even if the exponent … Read more

Creating tar file and naming by current date

Like this: name=$(date ‘+%Y-%m-%d’) tar -zcvf “$name.tar.gz” code or even in one line: tar -zcvf “$(date ‘+%Y-%m-%d’).tar.gz” code Drop -z flag if you want .tar instead of .tar.gz. Use %y instead of %Y if you want just 2 digits of a year (17 instead of 2017). $() is used for command substitution.

How can I format the output of a bash command in neat columns

column(1) is your friend. $ column -t <<< ‘”option-y” yank-pop > “option-z” execute-last-named-cmd > “option-|” vi-goto-column > “option-~” _bash_complete-word > “option-control-?” backward-kill-word > “control-_” undo > “control-?” backward-delete-char > ‘ “option-y” yank-pop “option-z” execute-last-named-cmd “option-|” vi-goto-column “option-~” _bash_complete-word “option-control-?” backward-kill-word “control-_” undo “control-?” backward-delete-char