Case-Insensitive Glob on Zsh/Bash

Case-insensitive Glob on zsh/bash

ZSH:

$ unsetopt CASE_GLOB

Or, if you don't want to enable case-insensitive globbing in general, you can activate it for only the varying part:

$ print -l (#i)(somelongstring)*

This will match any file that starts with "somelongstring" (in any combination of lower/upper case). The case-insensitive flag applies for everything between the parentheses and can be used multiple times. Read the manual zshexpn(1) for more information.

UPDATE
Almost forgot, you have to enable extendend globbing for this to work:

setopt extendedglob

Have zsh return case-insensitive auto-complete matches, but prefer exact matches

Just uncomment the following line in ~/.zshrc:

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

It worked for me

How to do a case insensitive for loop in shell scripting

In bash, you can use

shopt -s nocaseglob

Case-insensitive matching with Eshell?

You can set the variable eshell-glob-case-insensitive to true to enable case-insensitive globbing in eshell.

Case insensitive comparison of strings in shell script

if you have bash

str1="MATCH"
str2="match"
shopt -s nocasematch
case "$str1" in
$str2 ) echo "match";;
*) echo "no match";;
esac

otherwise, you should tell us what shell you are using.

alternative, using awk

str1="MATCH"
str2="match"
awk -vs1="$str1" -vs2="$str2" 'BEGIN {
if ( tolower(s1) == tolower(s2) ){
print "match"
}
}'

Sorting glob qualifiers do nothing in zsh

Charles Duffy is correct, it is ls that is re-sorting the files alphabetically. We can see this using his suggestions or simply by using echo.

ls has its own sorting flags, but I wanted to commit just one syntax to memory. Filename sorting in ls can be disabled with the -U flag.

It’s odd, because several popular guides to zsh that I consulted all use sorting examples with ls. Has ls changed its behavior recently? Is the version distributed with Debian unusual?

turn off ZSH glob expansion in function arguments in interactive shell

Use the noglob modifier.

% noglob § 3**4 - 2/7*5
81

To avoid having to type noglob each time, use an alias to insert the modifier. (You can't put noglob inside the function, as pathname expansion will have already taken place before the body is evaluated.)

% alias §='noglob §'
% § 3**4 - 2/7*5
81

Bash Regex match is case insensitive

Ok, found it. The reason was that nocaseglob was set.

Also dug a bit into the source code:

nocaseglob is defined in ./builtins/shopt.def which sets the internal variable glob_ignore_case.

This one in return is used in ./lib/sh/shmatch.c in the if-statement if (glob_ignore_case || match_ignore_case) which determines if the flag REG_ICASE should be set for the call to regcomp.

But according to the documentation on shopt nocaseglob only determines if filenames are match case-insensitiv, while I expected nocasematch to be responsible for regex-matching:

nocaseglob: If set, Bash matches filenames in a case-insensitive fashion when performing filename expansion.

nocasematch: If set, Bash matches patterns in a case-insensitive fashion when performing matching while executing case or [[ conditional commands, when performing pattern substitution word expansions, or when filtering possible completions as part of programmable completion.


Tracked to at least bash-3.0 which introduced the file shmatch.c to the current master version 5.0.17(2)-release. Fixed in devel branch 5.1.0(3)-alpha by commit aa99ef520 with the changelog

lib/sh/shmatch.c
- sh_regmatch: implement a suggestion from Grisha Levit
and don't allow nocaseglob to enable case-insensitive regexp matching. It hasn't been documented that way
in years



Related Topics



Leave a reply



Submit