Setting the Mac Terminal titlebar to the current working directory

I often work with multiple Mac Terminal tabs open, and as a result I like to set the title in the titlebar to whatever I’m working on in each tab (such as “MONGO” or “PLAY”), so I wrote a little script named settitle to let me set the title in the titlebar from the Unix command line.

More recently I realized I was often setting the title to the uppercase version of the current directory. I came up with this command pipeline (I like to call them “mashups”) to do easily do that:

settitle `pwd | xargs basename | tr '[a-z]' '[A-Z]'`

The basename command doesn’t accept args in a pipeline, so I used xargs to handle that problem. The tr command makes the directory name uppercase.

settitle and other commands

The settitle script is easy:

#!/bin/sh

# name:    settitle
# purpose: set the Mac Terminal title
# usage:   to set the titlebar to 'PLAY', type:  settitle PLAY

echo "\033]0;${1}\007\c"

xargs, basename, and tr are all standard Unix commands.

macOS changes

The latest versions of macOS make this a little more difficult, but if you go into the macOS Terminal preferences, then go to the “Tab” settings and un-check the defaults (un-check all of the checkboxes that affect the tab title), this script will work as advertised.