To do this, just add the following line to the end of your ~/.bash_profile:
ps ax|grep -q "[ ]`which X`" || startx
OR you can add this line instead:
ps ax|grep -q "[ ]`which X`" || xinit >/dev/null 2>&1
The last one is faster, because it starts xinit right away. Startx calls on xinit anyway, so this one saves a process. There are differences though. Startx uses .xsession and .Xresources; xinit uses .xinitrc and .Xdefaults. The files have exactly the same function.
You can even save some more by adding exec before xinit:
ps ax|grep -q "[ ]`which X`" || exec xinit >/dev/null 2>&1
The use of exec prevents the shell from running after X starts.
gunnix 01-26-2005