hello everyone, I am fuzzy about how to set a command in a script to be run only when the shell is running within an X session.

basically, in ~/.bashrc I set my keyboard maps as

setxkbmap -layout 'us,gr' -variant 'altgr-intl,extended' -option grp:alt_shift_toggle

If I am connecting through putty , or otherwise, and i just open a command prompt window, I DONT want this command to run. If on the other hand, i have an X session running (locally or remotely) I want this command to run.

how can I do this checking in a bash script? Is there a bash environment variable I can be looking at? some other way?

Thank you for your help

asked 22 Oct '12, 19:05

synthnassizer's gravatar image

accept rate: 100%

While ranton's answer is generic and adequate, for the specific case of setxkbmap (which is required to be run only during Xsessions) a more appropriate place holder for the above command is in the window managers xinitrc file. this should be ~/.xinitrc but different wm's place it elsewhere.

fluxbox has it in ~/.fluxbox/startup vnc sessions have it ~/.vnc/xstartup

The above is not complete, but they give you an idea of where to look for these files.


answered 07 Dec '12, 05:32

synthnassizer's gravatar image

accept rate: 100%

Well, normally I would say check the TERM environment variable, but Putty configurations often set this to xterm just like an actual xterm so that's not much help.

However, depending on your exact needs you can make PUTTY set an environment variable of your own choosing which you can check for by setting it in your Putty session configuration under Connection --> Data.


answered 31 Oct '12, 14:36

ranton's gravatar image

accept rate: 0%

Check the DISPLAY variable. If it's set, you're in an X session:

[ -n "$DISPLAY" ] &&
 echo "X session" ||
 echo "Not in X"

answered 02 Mar '13, 20:30

cfajohnson's gravatar image

accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 22 Oct '12, 19:05

Seen: 1,665 times

Last updated: 02 Mar '13, 20:30

powered by OSQA