tags:

views:

82

answers:

2

Is there a good way to test whether I am logging into a text shell or starting a GUI session in my .bashrc? For example, to set my editor to gedit if in gnome and emacs if using a command line.

+9  A: 

Your DISPLAY variable will be set if you're logged in to an X session.

Edit: So, this (untested) code should work:

[ -n "${DISPLAY}" ] && export EDITOR=gedit || export EDITOR=emacs

Fixed based on comments.

eduffy
Actually, you need to either leave off the -z or swap the editors.
Dennis Williamson
Replacing -z with -n would also work.
Michael E
Whoops .. thanks.
eduffy
FYI. [ is actually a command. If you're using bash, the preferred method is [[.
Jeremy Cantrell
+2  A: 

Using bash conventions:

if [[ $DISPLAY ]]; then
    export EDITOR=gedit
else
    export EDITOR=emacs
fi
Jeremy Cantrell