In Linux system, we can complete various functional programs by shell script.
What I want to record today is how to use the Dictionary (A key-value data), the so-called hash table in Bash.
No need to mention how useful the hash table is, in fact, the method of use is very simple.
The following is a simple sample code:
#!/bin/bash declare -A dict dict=( ['a']=1 ['b']=2 ['c']=3 ) for item in "${!dict[@]}" do echo "$item => ${dict[$item]}" done
Output:
a => 1
b => 2
c => 3
An error occurred: Syntax error: “(” unexpected
If the following error occurs during execution:
declare: not found
Or
Syntax error: "(" unexpected
It is very possible that your sh soft link does not point to bash, that is, you may not use bash to execute your program, you can use the following command to check:
ls -l /bin/sh
If the link you show is not bash (such as dash):
lrwxrwxrwx 1 root root 4 一 1 2020 /bin/sh -> dash
Then it means that the sh command does not use bash, you can consider directly using bash to execute the program, or switch the default sh to bash:
sudo dpkg-reconfigure dash
Then you will see the following screen:

If No is selected here, sh will be soft-linked to bash.
References
- https://stackoverflow.com/questions/1494178/how-to-define-hash-tables-in-bash
- https://askubuntu.com/questions/976485/what-is-the-point-of-sh-being-linked-to-dash