Merge branch 'pr-998'

This commit is contained in:
Rico Sta. Cruz 2019-03-23 22:26:48 +08:00
commit 34839f27d8
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 47 additions and 0 deletions

47
bash.md
View File

@ -472,6 +472,53 @@ for i in "${arrayName[@]}"; do
done done
``` ```
Dictionaries
------------
{: .-three-column}
### Defining
```bash
declare -A sounds
```
```bash
sounds[dog]="bark"
sounds[cow]="moo"
sounds[bird]="tweet"
sounds[wolf]="howl"
```
Declares `sound` as a Dictionary object (aka associative array).
### Working with dictionaries
```bash
echo ${sounds[dog]} # Dog's sound
echo ${sounds[@]} # All values
echo ${!sounds[@]} # All keys
echo ${#sounds[@]} # Number of elements
unset sounds[dog] # Delete dog
```
### Iteration
#### Iterate over values
```bash
for val in "${sounds[@]}"; do
echo $val
done
```
#### Iterate over keys
```bash
for key in "${!sounds[@]}"; do
echo $key
done
```
Options Options
------- -------