uppercase to lowercase in bash on a mac

Mala picture Mala · Mar 24, 2010 · Viewed 16.3k times · Source

I'm writing a bash script which needs to convert a string to lowercase. The problem is I'm doing it on a mac so 'tr' is not available. How can I go about doing this on a mac?

The problem I'm trying to tackle is that my script needs to recognize an if an extension is a .gif or a .jpg - and I don't want to have to check for .jpeg, .jPeg, .JPEG, .JPeg, etc etc etc... if there's a smarter way of doing this than just converting to lowercase and testing for gif, jpg and jpeg, i'm all ears :)

UPDATE:
I am an idiot.
The reason this mac "doesn't have" these basic text-conversion programs is because I overwrote PATH with "hello" when doing some testing >_<

Answer

ghostdog74 picture ghostdog74 · Mar 24, 2010

in bash, you can use nocaseglob

shopt -s nocaseglob
for file in *.jpg *.jpeg *.gif
do
  echo "$file"
done
#turn off
shopt -u nocaseglob

in general to convert cases, various ways

echo "stRING" | awk '{print toupper($0)}'

echo "STRING" | tr "[A-Z]" "[a-z]" # upper to lower

echo "StrinNG" | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' #lower to upper