bashuppercaselowercase

shorter way to check a .pgn non case sensitive extension in bash


I am writing a bash program that searches for .pgn (portable game notation) files in a folder. But in my program, .pgn extensions are not case sensitive. It means that they could be either lowercase or uppercase or a combination of the two. (.Pgn, .PGN, .pGn, .etc).
So for checking the extension I do this :

extension="${file: -4}"
extension="${extension,,}"
if [ "${extension}" == ".pgn" ]

I am wondering if there is a way to do this, in a shorter way. By shorter, I mean do it all in the if statement.
Thank you in advanced for helping me.


Solution

  • With bash and a regex:

    if [[ "$file" =~ \.[Pp][Gg][Nn]$ ]]
    

    Or output content of $file in lowercase and then compare only with lowercase:

    if [[ "${file,,}" =~ \.pgn$ ]]