Answers to: how to check in a bash script where the shell is run fromhttp://linuxexchange.org/questions/2953/how-to-check-in-a-bash-script-where-the-shell-is-run-from<p>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.</p> <p>basically, in ~/.bashrc I set my keyboard maps as </p> <blockquote> <p>setxkbmap -layout 'us,gr' -variant 'altgr-intl,extended' -option grp:alt_shift_toggle</p> </blockquote> <p>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.</p> <p>how can I do this checking in a bash script? Is there a bash environment variable I can be looking at? some other way?</p> <p>Thank you for your help</p>enSat, 02 Mar 2013 20:30:10 -0500Answer by cfajohnsonhttp://linuxexchange.org/questions/2953/how-to-check-in-a-bash-script-where-the-shell-is-run-from/3051<p>Check the DISPLAY variable. If it's set, you're in an X session:</p> <pre><code>[ -n "$DISPLAY" ] &amp;&amp; echo "X session" || echo "Not in X" </code></pre>cfajohnsonSat, 02 Mar 2013 20:30:10 -0500http://linuxexchange.org/questions/2953/how-to-check-in-a-bash-script-where-the-shell-is-run-from/3051Answer by synthnassizerhttp://linuxexchange.org/questions/2953/how-to-check-in-a-bash-script-where-the-shell-is-run-from/2991<p>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.</p> <p>fluxbox has it in ~/.fluxbox/startup vnc sessions have it ~/.vnc/xstartup</p> <p>The above is not complete, but they give you an idea of where to look for these files.</p>synthnassizerFri, 07 Dec 2012 05:32:51 -0500http://linuxexchange.org/questions/2953/how-to-check-in-a-bash-script-where-the-shell-is-run-from/2991Answer by rantonhttp://linuxexchange.org/questions/2953/how-to-check-in-a-bash-script-where-the-shell-is-run-from/2959<p>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.</p> <p>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 --&gt; Data.</p>rantonWed, 31 Oct 2012 14:36:42 -0400http://linuxexchange.org/questions/2953/how-to-check-in-a-bash-script-where-the-shell-is-run-from/2959