37 lines
831 B
Bash
37 lines
831 B
Bash
|
|
#!/bin/bash
|
||
|
|
|
||
|
|
#-------------------------------------------------------------------------------
|
||
|
|
# Example 1 for Bash Tips show 16: the difference between '*' and '@' as array
|
||
|
|
# subscripts
|
||
|
|
#-------------------------------------------------------------------------------
|
||
|
|
|
||
|
|
#
|
||
|
|
# Initialise an array
|
||
|
|
#
|
||
|
|
declare -a words
|
||
|
|
|
||
|
|
#
|
||
|
|
# Populate the array. Omit capitalised words and the weird possessives.
|
||
|
|
# [Note: there are better ways of populating arrays as we'll see in a later
|
||
|
|
# show]
|
||
|
|
#
|
||
|
|
for word in $(grep -E -v "(^[A-Z]|'s$)" /usr/share/dict/words | shuf -n 5); do
|
||
|
|
words+=( "$word" )
|
||
|
|
done
|
||
|
|
|
||
|
|
#
|
||
|
|
# Report the array using '*' as the index
|
||
|
|
#
|
||
|
|
echo 'Using "${words[*]}"'
|
||
|
|
for word in "${words[*]}"; do
|
||
|
|
echo "$word"
|
||
|
|
done
|
||
|
|
|
||
|
|
#
|
||
|
|
# Report the array using '@' as the index
|
||
|
|
#
|
||
|
|
echo 'Using "${words[@]}"'
|
||
|
|
for word in "${words[@]}"; do
|
||
|
|
echo "$word"
|
||
|
|
done
|