1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Transform Text To Lowercase as a Unix TIL

This commit is contained in:
jbranchaud
2025-10-17 21:49:38 -05:00
parent 5b3f1536fd
commit 5eb21b3aa2
2 changed files with 35 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1661 TILs and counting..._
_1662 TILs and counting..._
See some of the other learning resources I work on:
- [Get Started with Vimium](https://egghead.io/courses/get-started-with-vimium~3t5f7)
@@ -1687,6 +1687,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Switch Versions of a Brew Formula](unix/switch-versions-of-a-brew-formula.md)
- [Tell direnv To Load The Env File](unix/tell-direnv-to-load-the-env-file.md)
- [Touch Access And Modify Times Individually](unix/touch-access-and-modify-times-individually.md)
- [Transform Text To Lowercase](unix/transform-text-to-lowercase.md)
- [Type Fewer Paths With Brace Expansion](unix/type-fewer-paths-with-brace-expansion.md)
- [Undo Changes Made To Current Terminal Prompt](unix/undo-changes-made-to-current-terminal-prompt.md)
- [Undo Some Command Line Editing](unix/undo-some-command-line-editing.md)

View 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.