help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Have Bash to do case insensitive operation on test


From: Andreas Kusalananda Kähäri
Subject: Re: Have Bash to do case insensitive operation on test
Date: Wed, 14 Apr 2021 11:33:49 +0200

On Wed, Apr 14, 2021 at 08:30:31AM +0000, Budi wrote:
> How to have Bash to do case insensitive operation on test:
> 
> $ n=Foo
> $ [ -e "$n" ] && echo $n exist
> 
> foo exist
> if it is:
> 
> $ ls
> foo   bar   baz


Bash is able to do case-insensitive globbing, but the code that you
posted does not use globbing at all.  This doesn't stop us from
performing a glob match though.

        shopt -s nocaseglob nullglob extglob

        set -- *("$n")
        if [ -e "$1" ]; then
                printf 'at least one name matches *(%s)\n' "$n"
        fi

The *(...) globbing pattern is an extended globbing pattern borrowed
from the ksh shell, enabled with the extglob shell option, and it would
match zero or more names from the given |-delimited list of patterns
(only one pattern is used here, from the string in $n).

The nullglob shell option makes sure that the pattern is removed if
there are no matches (not really needed here), and nocaseglob is for
doing case-insenitive globbing.

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]