Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 1 down vote accepted

you can add it to ~/.xinitrc (when an X is about to start, whatever is included in this file is executed)

-or /etc/xinitrc for global -

the other way you can do it is to add the configuration inside xorg.conf (check how to change layout)

share|improve this answer
i'll try it and get back to you. – nass Oct 23 '12 at 10:42
hmmm i'm running fluxbox. and ~/.xinitrc doesn't seem to be sourced..... same with global xinitrc. I am not even sure the xorg.conf folder will work at this point – nass Oct 23 '12 at 11:08
hmm fluxbox does all starting up from ~/.fluxbox/startup. I put the command in there (and appended a &) and it works! – nass Oct 23 '12 at 11:20
ok ... yes ... it depends on the configuration, but the general idea is to put it in an X init script :> – Nikolaidis Fotis Oct 23 '12 at 11:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.