Mingfei Gao
Mingfei Gao

Reputation: 177

How to let bash/zsh glob case sensitive

Environment:

$ /usr/bin/zsh --version
zsh 5.5.1 (x86_64-redhat-linux-gnu)

$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

I want the glob result to be sorted in dictionary order (ASCII order) and be case-sensitive.

Current behavior:

# Both zsh and bash are the same
$ echo *
9.0 9.1 a.0 A.0 a.1 A.1

A.1 should be sorted after A.0.

Useless attempts:

# Both zsh and bash are the same
$ (LC_COLLATE=C; echo *)
9.0 9.1 a.0 A.0 a.1 A.1

# For zsh
$ (LC_COLLATE=C; echo *(on))
9.0 9.1 a.0 A.0 a.1 A.1

# For zsh
$ (LC_COLLATE=C; echo *(:o))
9.0 9.1 a.0 A.0 a.1 A.1

Upvotes: 1

Views: 71

Answers (2)

Philippe
Philippe

Reputation: 26727

You must have LC_ALL set, try in bash:

(unset LC_ALL; export LC_COLLATE=C; echo *)

Upvotes: 2

Toby Speight
Toby Speight

Reputation: 30930

If you want POSIX order, then you need to set LC_COLLATE in the shell that's doing the expanding:

(LC_COLLATE=C; echo *)

This gives

9.0 9.1 A.0 A.1 a.0 a.1

Note that using ls to inspect the result of expansion is a poor choice, as that does its own sorting for display. The GNU implementation accepts -U to disable that, but I think it's clearer to use echo as demonstration (above).

Upvotes: 1

Related Questions