Sort with case-sensitivity (lowercase before uppercase) in macOS in the Terminal

Sort order is defined in your locale.

locale            
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=

When LC_COLLATE is defined as LC_COLLATE="en_US.UTF-8" on macOS, LC_COLLATE uses the C or POSIX definiton. The default behavior of sort is to sort in ascii order where uppercase has precedence over lowercase.

On Linux, when LC_COLLATE="en_US.UTF-8" the LC_COLLATE definiton is non-standard. The default behavior of sort is to ignore-case and lowercase has precedence over uppercase.

To mimic Linux default sort behavior on macOS, You can use tr to "translate" lowercase letters to uppercase and uppercase to lowercase, sort with the ignore-case option (-f), then change the case back again.

tr 'a-zA-Z' 'A-Za-z' <file.txt | sort -f | tr 'a-zA-Z' 'A-Za-z'

EXAMPLE:

cat file.txt
"cat"
"Best"
"A"
"BEST"
"Castro"
"alfred"
"a"
"CAT"

tr 'a-zA-Z' 'A-Za-z' <file.txt | sort -f | tr 'a-zA-Z' 'A-Za-z'
"a"
"A"
"alfred"
"Best"
"BEST"
"Castro"
"cat"
"CAT"