Use tr to implement toupper/tolower in Bash
I'm always comparing Bash to Python for everyday scripting tasks. Bash is deeply beautiful in a lot of ways but the Python library is incredible. Bash should have integrated this from day one.
toupper() {
local char="$*"
out=$(echo $char | tr [:lower:] [:upper:])
local retval=$?
echo "$out"
unset out
unset char
return $retval
}
The situation improves after 3.2.25 via parameter expansion:
var="hello world"; echo ${var^^} # toupper()
var="hello world"; echo ${var,,} # tolower()
Written by Chad Colgur
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Shell
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#