mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Transform Text To Lowercase as a Unix TIL
This commit is contained in:
33
unix/transform-text-to-lowercase.md
Normal file
33
unix/transform-text-to-lowercase.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# Transform Text To Lowercase
|
||||
|
||||
I was reading through [`setup.sh` in
|
||||
dkarter/dotfiles](https://github.com/dkarter/dotfiles/blob/master/setup.sh#L7-L9)
|
||||
and noticed this function for converting a given bit of text to all lowercase
|
||||
letters.
|
||||
|
||||
```bash
|
||||
lowercase() {
|
||||
echo "$1" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/"
|
||||
}
|
||||
```
|
||||
|
||||
It's an interesting use of `sed`, but it made me wonder if `tr` was a better
|
||||
tool for this job. I looked into it and `tr` is better suited to the task, more
|
||||
expressive, and also compatible across Mac and Linux.
|
||||
|
||||
Here is what it looks like with `tr`:
|
||||
|
||||
```bash
|
||||
lowercase() {
|
||||
echo "$1" | tr '[:upper:]' '[:lower:]'
|
||||
}
|
||||
```
|
||||
|
||||
This has the added benefit of working across all kinds of UTF-8 characters.
|
||||
|
||||
```bash
|
||||
$ echo "ΑΛΦΑΒΗΤΟ ΕΛΛΑΔΑ" | tr '[:upper:]' '[:lower:]'
|
||||
αλφαβητο ελλαδα
|
||||
```
|
||||
|
||||
See `man tr` for more details.
|
||||
Reference in New Issue
Block a user