Search code examples
arraysbashreplacecase-insensitive

Case insensitive search and replace in array bash array


I learned here about search and replace in array using array.

It goes like this.

declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora');
echo ${Unix[@]/Red*/}

# Output will be
Debian Ubuntu Suse Fedora

In the above snippet, flag a was used to do this with case-insensitive search. i.e I want to search with "red" instead "Red".


Solution

  • Just put r and R inside a character class [].

    $ declare -a Unix=('Debian' 'red hat' 'Ubuntu' 'Suse' 'Fedora');
    $ echo ${Unix[@]/[rR]ed*/}
    Debian Ubuntu Suse Fedora