#!/bin/bash #------------------------------------------------------------------------------- # Example 5 for Bash Tips show 17: Trimming leading or trailing parts #------------------------------------------------------------------------------- # # Make an indexed array of root vegetables # declare -a vegs=(celeriac artichoke asparagus parsnip mangelwurzel daikon turnip) printf '%s\n\n' "${vegs[*]}" # # Demonstrate some trimming # echo "1. Removing the first character:" echo "${vegs[@]#?}" echo "2. Removing characters up to and including the first vowel:" echo "${vegs[@]#*[aeiou]}" echo "3. Removing characters up to and including the last vowel:" printf '[%s] ' "${vegs[@]##*[aeiou]}" echo echo "4. Using an extglob pattern to remove several different leading patterns:" shopt -s extglob echo "${vegs[@]#@(cele|arti|aspa|mangel)}" echo "5. Removing the last character": echo "${vegs[@]%?}" echo "6. Removing from the last vowel to the end:" echo "${vegs[@]%[aeiou]*}" echo "7. Removing from the first vowel to the end:" printf '[%s] ' "${vegs[@]%%[aeiou]*}" echo echo "8. Using an extglob pattern to remove several different trailing patterns:" echo "${vegs[@]%@(iac|oke|gus|nip|zel)}"