#!/bin/bash #------------------------------------------------------------------------------- # Example 2 for Bash Tips show 18: transforming words in a string, second # simpler method that depends on the '-v' operator #------------------------------------------------------------------------------- # # What we'll work on and where we'll store the transformed version # phrase='Now is the time for all good men to come to the aid of the party' newphrase= # # How to transform words in the phrase # declare -A transform=([good]='bad' [men]='people' [aid]='assistance' [party]='Community') # # Go word by word; if an element with the word as a key exists in the # 'transform' array replace the word by what's there. # for word in $phrase; do if [[ -v transform[$word] ]]; then word=${transform[$word]} fi newphrase+="$word " done echo "$newphrase" exit