user80168
user80168

Reputation:

Locale-aware uppercase in Bash?

How to make locale aware uppercase operation?

Standard tr '[:lower:]' '[:upper:]' trick doesn't work:

=$ echo żółw | tr "[:lower:]" "[:upper:]"
żółW

(it should be ŻÓŁW)

I'd rather avoid having to run Perl or anything such heavy-weight.

Upvotes: 2

Views: 640

Answers (2)

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340301

It seems to me that if you want to avoid a heavyweight you'd need to provide the characters yourself, and, given that's a nonstarter for a truly generic solution, you are stuck with using a heavyweight.

One heavyweight solution

vinko@parrot:~$ more t.py
# coding: UTF-8
print unicode.upper("żółw".decode('utf-8'))
vinko@parrot:~$ python t.py
ŻÓŁW

Non-heavyweight solution that would require you to specify each character

vinko@parrot:~$ echo żół | tr "[żół]" "[ŻÓŁ]"
ŻÓŁ

EDIT: Based on the other answer and comments, BASH 4.0 IS locale sensitive and aware of wide chars. You have to set a proper locale of course (LC_CTYPE or LC_ALL) so BASH can tell what it is supposed to do. Also it seems that there are locale sensitive versions of tr as of late (for example, Mac OSX 10.6)

Upvotes: 1

Chris Johnsen
Chris Johnsen

Reputation: 224689

If you bash is new enough, it may be able to do it (see bash-4.0 NEWS item 1.hh.).

bash -c 'foo="żółw"; echo ${foo^^}'

zsh, too (since 4.3.3?):

zsh -c 'foo="żółw"; echo ${(U)foo}'

Upvotes: 2

Related Questions