I am trying to create an associative array in Bash in the following way:
#!/bin/bash
hash["name"]='Ashwin'
echo ${hash["name"]}
This prints the desired output, Ashwin, when executed.
But when the key has a space in it,
#!/bin/bash
hash["first name"]='Ashwin'
echo ${hash["first name"]}
I get the following error
test2.sh: line 2: first name: syntax error in expression (error token is "name")
Are keys not allowed to have spaces in them?
If you first use declare -A hash
before the value assignments, then the script runs as expected.
It was tested using Bash 4.2.25.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With