Easier use of X11 in bash

Oct 30, '03 09:26:00AM

Contributed by: Anonymous

I come from a Unix backbround and use a lot of X11 only apps in my daily work, so I was pretty excited when Apple released X11 for OSX and has now included it with Panther. What follows are a couple of changes to bash's startup scripts (this applies to Panther users only, unless you change your default shell to bash on 10.1 or 10.2) to make using X11 apps more convenient from Terminal.app.

Read the rest of the hint for the changes...

First, a simple change to ~/.bashrc (since Apple included X11, but didn't add the X11 apps to the default PATH):


PATH=${PATH}:/usr/X11R6/bin
export PATH
Then, a whole lot of changes to ~/.bash_profile:

# source .bashrc if it's there
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi
# if we're NOT ssh'd in
if [ ! ${SSH_TTY} ]; then
  # make sure X is running
  if [ "x`ps -x | awk '{print $5}' | grep X11`" = "x" ]; then
    open /Applications/Utilities/X11.app
    # then refocus Terminal.app
    osascript << EOF
      tell application "Terminal"
        activate
      end tell
    EOF
  fi
  # if DISPLAY isn't set
  if [ x${DISPLAY} = x ]; then
    export DISPLAY=:0
  fi
fi
# I like ls to color code my files
export CLICOLOR=1
What we do here is first check that this is not a remote ssh session. I tend to use a lot of X11 apps over ssh connections using ssh's built-in X11 forwarding. So, if I'm ssh'd in, I don't want to start up X, since I won't be displaying locally, it doesn't matter. I also don't want to clobber DISPLAY, since ssh sets up it's own DISPLAY variable for tunneling (generally localhost:10).

If we're not in an ssh session, I first make sure X11 is running and start it up if it's not. Then, using a tiny AppleScript (which represents all of the AppleScript I've ever written), I refocus the Terminal.

Finally, I make sure that we have a DISPLAY environment variable, since X11 apps won't run if they don't know which DISPLAY to use.

I hope somebody else out there finds this useful.

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20031026091804549