Last Updated: February 25, 2016
·
568
· colgur

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()