1. Always use ShellCheck
2. -o pipefail
is undefined
Remove it
3. echo
flags are undefined
Probably easiest to remove ANSI codes.
4. &>/dev/null
is a Bash-ism
Replace with >/dev/null 2>&1
.
5. function
keyword does not exist
Replace function NAME { }
with NAME() { }
.
6. [[
is a Bash test
Replace [[ ... ]]
with [ ... ]
.
7. Arrays don't exist
Associative arrays can be approximated with careful use of eval
.
Define a variable with an array name as a prefix, and the key as a suffix:
NAME_KEY='VALUE'
It is convenient to have a function that converts strings to identifiers:
# identifier STR # Replace characters that are not valid in an identifier with underscores. identifier() { echo "$1" | tr -C '[:alnum:]_\n' '_' }
Define a function that retrieves a key KEY from an array ARRAY:
# aget ARRAY KEY # Get the value of KEY from associative array ARRAY. # Characters that are not valid in an identifier are replaced with underscores. aget() { eval echo '$'"$(identifier "$1")_$(identifier "$2")" }
Use the aget
function as in the following:
aget NAME KEY
Producing the following output:
VALUE
8. No local
keyword
Remove them and be careful with naming.
9. No HERESTRINGs
Replace <<<
with a HEREDOC:
x="variable" cat <<POSIX_HERESTRING $x POSIX_HERESTRING