Skip to content

Instantly share code, notes, and snippets.

@ErnWong
Created September 11, 2020 23:57
Show Gist options
  • Save ErnWong/ee6b2aa06b7064dee853e6e2c9872780 to your computer and use it in GitHub Desktop.
Save ErnWong/ee6b2aa06b7064dee853e6e2c9872780 to your computer and use it in GitHub Desktop.
Potential bash test -v bug with associative arrays
#!/usr/bin/env bash
declare -A myarray
myarray["'"]="yes"
echo ${!myarray[@]}
echo ${myarray["'"]}
[[ -v myarray["'"] ]] && echo yes || echo no # Expected: "yes" printed. Actual: "no" printed.
# Tested on GNU bash version 4.4.20(1)-release
@ErnWong
Copy link
Author

ErnWong commented Sep 11, 2020

Looks like [[ -v myarray["\'"] ]] works even though it doesn't need escaping. The problem is when the key is obtained from a variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment