Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Easier use of X11 in bash UNIX
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.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[15,535 views]  

Easier use of X11 in bash | 11 comments | Create New Account
Click here to return to the 'Easier use of X11 in bash' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Easier use of X11 in bash
Authored by: denken on Oct 30, '03 11:49:08AM

bash pukes on the line that determines if X11 is already running or not... something to do with how characters are escaped im guessing? trying to figure it out now...



[ Reply to This | # ]
Easier use of X11 in bash
Authored by: virga42 on Oct 30, '03 01:30:07PM

I modified the script to:

#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 [ "`ps -x | awk '{print $5}' | grep X11`" = "" ]; then
open /Applications/Utilities/X11.app
# then refocus Terminal.app
osascript -e 'tell application "Terminal" to activate'
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

I removed the "x"s in line 8 and adjusted the osascript line to make it a little more compact.

The problem I have found is that you regain focus of Terminal.app briefly and then X11.app steals focus back when it opens an xterm window.



[ Reply to This | # ]
Easier use of X11 in bash
Authored by: denken on Oct 30, '03 04:23:56PM

1. cp /etc/X11/xinit/xinitrc ~/.xinitrc

2. hash out the line that starts an xterm

Now you wont loose focus of Terminal.app when X11 starts. I think the whole idea here was to use Terminal.app instead of a regular xterm, but still have X11 running so you can launch X11 apps from Terminal.app.

Then again, maybe you have a reason to have an xterm open. Of course, if that is the case then you dont really need all this Terminal.app initialization stuff :)



[ Reply to This | # ]
Easier use of X11 in bash - script fix
Authored by: ophir on Oct 30, '03 03:17:28PM

It seems to be puking on the osascript section. If you move the second EOF to the beginning of the line that it is on, the script works fine.



[ Reply to This | # ]
Easier use of X11 in bash
Authored by: GaelicWizard on Oct 30, '03 04:52:14PM

Some tips on bash from a fellow rookie:

The reason that .bashrc is not sourced when you open a new terminal, but it is when you open an xterm, is that terminal.app launches a "login shell" by default, where as xterm does not.

What's the diff? In a "login shell" bash sources ~/.bash_profile or ~/.bash_login or ~/.profile (whichever one it finds first), and (t)csh would source ~/.login. This is primarily for setting up things like PATH and other env variables that should really only need to be set once. For non-login shells, bash sources only ~/.bashrc ((t)csh sources ~/.(t)cshrc in both) to set up things that are for the general use shell, or a shell script, this means generally aliases and setting up the prompt line etc. the login shell is, by definition, only run by a user, so you can get away with printing loads of stuff to the screen, where in general shell a shell script might misunderstand the screen output.

I use the amazingly useful and wonderful 'screen' program to multiplex my terminal screens, and this also makes sure that both ~/.bash_profile AND ~/.bashrc are sourced since bash starts as a login shell in terminal.app (sourcing ~/.bash_profile) and then screen takes over and in turn loads a normal shell (which sources ~/.bashrc).

Just thought I'd share this little info 'cuz I noticed that the original author source'd ~/.bashrc in his ~/.bash_profile. :-)

JP

---
Pell



[ Reply to This | # ]
Easier use of X11 in bash
Authored by: 0ri0n on Oct 30, '03 05:57:26PM

Well, I don't understand my output (I am a newbie). I used the script as shown and I get the following:

-bash: exportPATH: command not found
-bash: /Users/isuma/.bash_profile: line 25: syntax error: unexpected end of file

Any ideas?



[ Reply to This | # ]
Easier use of X11 in bash
Authored by: denken on Oct 30, '03 06:29:25PM

see my earlier post and virga42's reply... there is a bit of syntax problem with the original post. i also gave a hint on setting up your .xinitrc file. on my panther system, this works:

# if we're NOT ssh'd in
if [ ! ${SSH_TTY} ]; then
# make sure X is running
if [ "`ps -x | awk '{print $5}' | grep X11`" = "" ]; then
open /Applications/Utilities/X11.app
# then refocus Terminal.app
osascript -e 'tell application "Terminal" to activate'
fi
# if DISPLAY isn't set
if [ x${DISPLAY} = x ]; then
export DISPLAY=:0
fi
fi

# Turn on pretty colors
export CLICOLOR=1



[ Reply to This | # ]
Easier use of X11 in bash
Authored by: 0ri0n on Oct 30, '03 06:56:11PM

should have tried your suggestion: it works!

Thanks!



[ Reply to This | # ]
Easier use of X11 in bash
Authored by: anodyne on Nov 04, '03 01:42:36PM

Well, I tried the suggestions and got this

Welcome to Darwin!
byu097411wks:~ david$ gimp
Xlib: connection to ":0.0" refused by server
Xlib: No protocol specified

Any suggestions



[ Reply to This | # ]
Easier use of X11 in bash
Authored by: kingtrench on Nov 02, '04 06:19:14PM
Here's a different solution for getting the DISPLAY variable correctly set in Terminal.app. This will also work with Fast User Switching, when DISPLAY is not necessarily :0.0 .

1. Put X11 in your startup items (System Preferences --> Accounts --> Startup Items)

2. Copy /private/etc/X11/xinit/xinitrc to .xinitrc in your home directory.

3. Edit .xinitrc so that the last few lines are:


# Record the DISPLAY variable so other programs can discover it
echo $DISPLAY > ~/.X11_DISPLAY

# start some nice programs

#xterm &

# start the window manager

exec quartz-wm

Basically we're putting the value of the DISPLAY variable in a file so we can find it from Terminal.app. Also, since I prefer to user Terminal.app instead of xterm in X11, I've commented out the automatic startup of xterm.

4. Add these lines to your .bashrc file:


# Is X11 running?  Is this an Apple Terminal?  Then set DISPLAY.
# Warning - DISPLAY must not be clobbered in xterm, ssh, etc...
if [ "$TERM_PROGRAM" == Apple_Terminal ] && [ -f ~/.X11_DISPLAY ] ; then
    export DISPLAY=`cat ~/.X11_DISPLAY`
fi

For this to work, .bashrc must be sourced when you start a new Terminal. This has been covered in a few places already, but what I do is to add these lines to .bash_profile:


if [ -f ~/.bashrc ] ; then
        . ~/.bashrc
fi

And this line to .Xdefaults:


XTerm*.LoginShell: True


[ Reply to This | # ]
Easier use of X11 in bash
Authored by: Oops on Jan 04, '05 05:10:59PM

Thank you! Thank you! Thank you! kingtrench!

I've recently switched to bash from tcsh and have not been able to get X11 forwarding to work, whereas I was successful with it before. Your suggestions in this post provided what I needed to get it to work again. Binaries were having difficulty getting the Display.



[ Reply to This | # ]