MacOS – How to make OS X’s bash expand $PWD to be case sensitive

bashmacosterminal

I am running OS X 10.8 on a MBP 2011. I use some bash-scripts for tasks like backups of remote servers. One of the scripts contains this snippet:

#!/bin/bash
if [ "$PWD" != /Users/myuser/Documents/Backup ]
then
    echo "Wrong path: $PWD"
    exit 0
fi

When I execute this script in /Users/myuser/Documents/Backup (note the upper case B), I get this response:

Wrong path: /Users/myuser/Documents/backup

For some reason, the $PWD gets expanded to a lower case name. How can I alleviate this? My filesystem is not case sensitive: both cd Backup and cd backup work.

Best Answer

You have two choices really: convert $PWD to a consistent case before comparing to your other string (which should also have consistent case). Or drop case-sensitive comparisons.

Converting it to lower case before comparing:

#!/bin/bash
if [ `echo $PWD | tr [:upper:] [:lower:]` != /users/myuser/documents/backup ]
then
    echo "Wrong path: $PWD"
    exit 0
fi

Dropping case sensitive comparisons:

#!/bin/bash
shopt -s nocasematch
if [[ "$PWD" != /Users/myuser/Documents/Backup ]]
then
    echo "Wrong path: $PWD"
    exit 0
fi