Merge branch 'pr-998'
This commit is contained in:
commit
34839f27d8
47
bash.md
47
bash.md
|
@ -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
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue