{"id":74,"date":"2013-06-24T04:55:55","date_gmt":"2013-06-24T08:55:55","guid":{"rendered":"https:\/\/rebrik.info\/blog\/?p=74"},"modified":"2013-06-25T06:00:21","modified_gmt":"2013-06-25T10:00:21","slug":"xubuntu-xfce-setting-shell-environment","status":"publish","type":"post","link":"https:\/\/rebrik.info\/blog\/?p=74","title":{"rendered":"Xubuntu + Xfce: setting shell environment"},"content":{"rendered":"<p><strong>System:<\/strong> Ubuntu 12.04 (precise)<br \/>\n<strong>Desktop Environment:<\/strong> Xfce 4.10, distributed by Xubuntu<\/p>\n<p>As it often happens, an update or install messes up your system and in the process of fixing it you start researching and learning something new. Which is a good thing. Bad thing is that you sure have other plans when you are forced to work on the fix.<\/p>\n<p>This time it was installation of Ruby enVironment Manager (rvm). Everything went smoothly at the time, but later I&#8217;ve noticed that my <span style=\"font-family: courier new,courier;\">~\/.bashrc<\/span> is no longer sourced. It turns out that rvm installed <span style=\"font-family: courier new,courier;\">.bash_profile<\/span>\u00a0 in my home dir (I did not have it before). Also the instructions suggested to run terminal in the login mode to make sure rvm is working. Now I know what happened and why, but it took me some time to figure it out. Below is what I learned and how I fixed and organized my shell environment in the process.<\/p>\n<p>A bit of theory first. Your shell can be: login\/non-login and interactive\/non-interactive. These options make 4 possible combinations. Login shell is used when the user is required to login, e.g. via ssh &#8211; usually it is also an interactive shell. In desktop environment terminal is normally started as an interactive non-login shell, but it is possible to configure the terminal to start as a login shell (despite the name it does not ask for user\/password when it starts). In Xfce: open a terminal and set the option using this path:\u00a0 Menu -&gt; Edit -&gt; Preferences -&gt; General Tab -&gt; &#8220;Run command as login shell&#8221;<\/p>\n<p>Here is how bash deals with the 3 files which can be present in the home dir (derived from &#8220;man bash&#8221;). Interactive login shell searches files in this order (only the first found is executed):<\/p>\n<p><span style=\"font-family: courier new,courier;\">~\/.bash_profile<\/span><br \/>\n<span style=\"font-family: courier new,courier;\"> ~\/.bash_login<\/span><br \/>\n<span style=\"font-family: courier new,courier;\"> ~\/.profile<\/span><\/p>\n<p>Interactive non-login shell uses only this one:<br \/>\n<span style=\"font-family: courier new,courier;\">~\/.bashrc<\/span><\/p>\n<p>To avoid surprises I&#8217;ve put all custom settings into <span style=\"font-family: courier new,courier;\">~\/.bashrc<\/span><br \/>\nand sourced it from <span style=\"font-family: courier new,courier;\">~\/.bash_profile<\/span><br \/>\nSomething like that:<\/p>\n<pre class=\"brush: bash;  gutter: false\"># if running bash\r\nif [ -n &quot;$BASH_VERSION&quot; ]; then\r\n\u00a0\u00a0\u00a0 # include .bashrc if it exists\r\n\u00a0\u00a0\u00a0 if [ -f &quot;$HOME\/.bashrc&quot; ]; then\r\n\u00a0\u00a0 \u00a0. &quot;$HOME\/.bashrc&quot;\r\n\u00a0\u00a0\u00a0 fi\r\nfi<\/pre>\n<p>This way I get the same environment in both login and non-login shells.<\/p>\n<p>After having done this I decided that I also want to have custom environment for the programs launched from the desktop environment (GUI). When Xfce session starts (right after you are authenticated in the initial login screen), Xfce tries to run the script from <span style=\"font-family: courier new,courier;\">~\/.config\/xfce4\/xinitrc<\/span>, if it is not found, it runs the script located in<span style=\"font-family: courier new,courier;\"> \/etc\/xdg\/xfce4\/xinitrc<\/span>. Below is the sequence of calls when the user&#8217;s script is found:<br \/>\n<span style=\"font-family: courier new,courier;\">root: \/sbin\/init<\/span><br \/>\n<span style=\"font-family: courier new,courier;\"> root: lightdm<\/span><br \/>\n<span style=\"font-family: courier new,courier;\"> root: lightdm &#8211;session-child 12 97<\/span><br \/>\n<span style=\"font-family: courier new,courier;\"> user:\u00a0 \/bin\/sh\/ \/home\/user\/.config\/xfce4\/\/xinitrc &#8212; \/etc\/X11\/xinit\/xserverrc<\/span><br \/>\n<span style=\"font-family: courier new,courier;\"> user: xfce4-session<\/span><br \/>\nThe last process launches everything related to the desktop environment, and its children inherit the settings provided in <span style=\"font-family: courier new,courier;\">xinitrc<\/span>. To customize the GUI environment you are supposed to copy the system file to your home dir and modify it:<\/p>\n<pre class=\"brush: bash; gutter: false\">cp \/etc\/xdg\/xfce4\/xinitrc ~\/.config\/xfce4\/xinitrc<\/pre>\n<p>Here is a part of the modified file:<\/p>\n<pre class=\"brush: bash; gutter: false\"># run xfce4-session if installed\r\nif which xfce4-session &gt;\/dev\/null 2&gt;&amp;1; then\r\n\r\n\u00a0 # check if we start xfce4-session with ck-launch-session. this is only\r\n\u00a0 # required for starting from a console, not a login manager\r\n\u00a0 if test &quot;x$XFCE4_SESSION_WITH_CK&quot; = &quot;x1&quot;; then\r\n\u00a0\u00a0\u00a0 if which ck-launch-session &gt;\/dev\/null 2&gt;&amp;1; then\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ck-launch-session xfce4-session\r\n\u00a0\u00a0\u00a0 else\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 echo\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;You have tried to start Xfce with consolekit support, but&quot;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;ck-launch-session is not installed.&quot;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 echo &quot;Aborted startup...&quot;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 echo\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 exit 1\r\n\u00a0\u00a0\u00a0 fi\r\n\u00a0 else\r\n\u00a0\u00a0\u00a0 # set gui environment\r\n\u00a0\u00a0\u00a0 if [ -f &quot;$HOME\/.xfcebashrc&quot; ]; then\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0source &quot;$HOME\/.xfcebashrc&quot;\r\n\u00a0\u00a0\u00a0 fi\r\n\u00a0\u00a0\u00a0 # start xfce4-session normally\r\n\u00a0\u00a0\u00a0 xfce4-session\r\n\u00a0 fi\r\n\r\n\u00a0 exit 0\r\nfi<\/pre>\n<p>The snippet above is almost identical to the original system file, I&#8217;ve only inserted these lines:<\/p>\n<pre class=\"brush: bash; gutter: false\">     # set gui environment\r\n \u00a0\u00a0\u00a0 if [ -f &quot;$HOME\/.xfcebashrc&quot; ]; then\r\n \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0. &quot;$HOME\/.xfcebashrc&quot;\r\n \u00a0\u00a0\u00a0 fi<\/pre>\n<p>Note that the script is run under sh, not bash, so bash-specific commands like <span style=\"font-family: courier new,courier;\">source<\/span> would not work. In the code above <span style=\"font-family: courier new,courier;\">~\/.xfcebashrc<\/span> is sourced, and I can configure my GUI environment by making it look like this:<\/p>\n<pre class=\"brush: bash; gutter: false\"># Local Java installation path\r\nPATH=$HOME\/bin\/jdk1.6.0_31\/bin:$PATH\r\nexport JAVA_HOME=$HOME\/bin\/jdk1.6.0_31\r\n\r\n# Android tools path\r\nPATH=$PATH:$HOME\/bin\/android-sdk-linux\/tools:\\\r\n$HOME\/bin\/android-sdk-linux\/platform-tools\r\n# Android NDK path\r\nPATH=$PATH:$HOME\/bin\/android-ndk\r\n\r\n# Add RVM to PATH for scripting\r\nPATH=$PATH:$HOME\/.rvm\/bin \r\n\r\nexport PATH<\/pre>\n<p>Now I have to take care of two files when I want to change shell environment:<br \/>\n<span style=\"font-family: courier new,courier;\">~\/.xfcebashrc<\/span>\u00a0\u00a0\u00a0 &#8211; for GUI &#8211; launched programs and<br \/>\n<span style=\"font-family: courier new,courier;\">~\/.bashrc<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8211; for the terminal<br \/>\nHaving two files I can set these environments independently. Note that the GUI environment will change on the next GUI login.<\/p>\n<p>And in the end some useful commands and resources found on the net.<br \/>\nTo see which path (or other environment) is being used by your GUI programs, start &#8220;Run Program&#8221; from the System menu and run this (it will produce a message containing the GUI path):<\/p>\n<pre class=\"brush: bash; gutter: false\">xmessage $PATH<\/pre>\n<p>To check if the shell is login or non-login:<\/p>\n<pre class=\"brush: bash; gutter: false\">shopt login_shell<\/pre>\n<p>To check environment of a running process with a known PID (replace PID with a number):<\/p>\n<pre class=\"brush: bash; gutter: false\">xargs --null --max-args=1 echo &lt; \/proc\/PID\/environ<\/pre>\n<p>Useful resources:<br \/>\n<a href=\"http:\/\/wiki.debian.org\/EnvironmentVariables\">http:\/\/wiki.debian.org\/EnvironmentVariables<\/a><br \/>\n<a href=\"http:\/\/wiki.debian.org\/DotFiles\">http:\/\/wiki.debian.org\/DotFiles<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>System: Ubuntu 12.04 (precise) Desktop Environment: Xfce 4.10, distributed by Xubuntu As it often happens, an update or install messes up your system and in the process of fixing it you start researching and learning something new. Which is a good thing. Bad thing is that you sure have other plans when you are forced [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/74"}],"collection":[{"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=74"}],"version-history":[{"count":10,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":87,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions\/87"}],"wp:attachment":[{"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rebrik.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}