Note: Mac OS X 10.3's open-x11 does not require this modification. It already launches the Apple X11 package.
The Apple X11 application opens up a world of graphical Unix tools to Mac OS X users. After downloading and installing the X11 release from Apple's site (as well as the corresponding SDK), I went to work using some of my favorite X11 apps, like xchat and pan. My only gripe was that the applications didn't quite open the way I'd expected them to.
If you attempt to run an X11 app from within a Terminal, you'll often get an error about an unknown display. This is because your Terminal session isn't aware of your X11 session. To fix this, Apple provides a script in
open-x11. This script sets the DISPLAY environment variable and then launches your chosen app. For example,
open-x11 xeyes will open the xeyes application in your X11 session.
This is all fine and dandy, but what if I forget to launch X11 before I run the
open-x11 script? I still get those same old errors. To fix this, I added a little bit to the
open-x11 script. You can find my version of the script below, but first a little explanation. I use the
grep commands to check if there are any files or TCP ports open by X11.app. If there aren't any, then I use the
open command to launch X11.app. The rest of the script is the same as the original, so it will set the necessary variables and open your Unix app.
# Make sure X11 is running first
lsof | grep X11 > /dev/null
if [ "$?" == "1" ]; then
# Script to run a program with the X path and display configured correctly
if [ "x$DISPLAY" = "x" ]; then
for d in /usr/X11R6/bin /usr/bin/X11 /usr/local/bin/X11; do
if [ -d $d ]; then
case $PATH in
: do nothing
# redirect stdio?
"$prog" "$@" &
man lsof, man grep