Here’s another gem for Linux Hacks that’s bugged me far too long. We start with a story. You’re new to Linux. (Maybe you aren’t but just pretend you are.) You start playing with some bash scripts eventually accumulating a collection of little knick-knacks and doo-hickeys. They’re living in a folder titled “scripts” in some odd location under your home folder. (Maybe under Documents or something.) You know those brilliant ideas you spend hours on in the beginning only to find out there was already a bash command or combination that does the same thing but you still use your homemade utility because it was sooo cool to write and you spent so much time and effort and you have to justify the time somehow so why not eat your own dog food even if it literally looks, tastes, and smells like Purina. Yeah those kind of scripts! After a while your scripts grow up and start doing big boy things like opening little dialogs or starting the beanshell gui in a certain directory with a preset classpath, or sending DCOP commands to your music player or whatever. You want to run them other places instead of from the command line. You’ve read some quick start bash tutorials and discovered that any command you add to your $PATH can be run without specifying it’s absolute folder location. You also learned about the magic files `/.profile, `/.bash_profile, and `/.bashrc that are read by the bash interpreter when you start a session. Maybe you came from Windows XP or Win2000 and you draw a parallel between these magic files and the system environment settings under “My Computer -> properties -> Advanced Settings”. Whatever the case you have the understanding that editing one or all of these files and setting the PATH variable accordingly will result in the desired behavior allowing you to run your commands directly from your desktop’s run dialog. (In KDE, Gnome, and I believe XFCE the run dialog is typically enchanted by summoning the Alt+F2 key sequence.) After setting your PATH and closing/re-opening your konsole, err command shell you are content to see the environment has been set to your liking. You later attempt to open your run dialog and directly enter your command which results in a round of random profanity and swearing. It doesn’t work!
Get to the point! How do we fix it???
I can’t jump right into the sauce without giving the background of what’s wrong. I’m not the brightest man in the world but I’m going to explain things to the best of my knowledge. The problem of your desktop (Gnome gdm, KDE kdm) not picking up your PATH settings in your home profile scripts stems from the fact that your desktop has not been run as a login shell. To understand a little better put the following command in your `/.bash_profile:
If fortune is unfortunately not installed on your system run “apt-get install fortunes” (or “apt-get install fortune” I can’t remember which one.) If you don’t “get” apt-get (some of you may be running Gentoo based or Suse with Yast) just look in your package manager for the fortune program. Try not to make this another project. Worst case use the following command instead:
echo "Your fortune: People who live in glass house should not throw stones. Throwing parties is an acceptable alternative." > ~/fortune
Now login to your system from the command line using “su <your_user_account>” You should not see anything different than usual. Now exit that shell and login again but this time with the magic dash ‘-‘ after the su command: “su – <your_user_account>” You should see your fortune echoed back to you. (If you’re running the Beryl desktop then flip the command shell around to the back and read me your lucky lotto numbers!) The dash character makes your new shell a login shell. In other words your .bash_profile is sourced on startup. That’s what’s missing by default in the window managers on many Linux distributions. To correct the problem we must make the window manager a login shell. (I’m not sure of the consiequences of such change. If there are side affects please let me know because I’m just as new to things as most people.) On my Mepis-6.5 system I navigate into my /etc/kde3/kdm/Xsession file and make my adjustment at the top. Originally it has the typical she-bang:
I introduce the login parameter:
For those who don’t know, the she-bang is the standard prelude to a bash script. It is the 1st line of the file that the system reads which tells it which interpreter to use to process the file. It’s like a mini command line for running the source file and it can be parameterized like any typical command line. Appending the -l (or –login) parameter to the /bin/sh command tells the system to use a login shell. That’s all there is to it. Restarting X (logging out from the run menu and hitting Ctrl+Alt+Backspace from the login screen) will then load my mindow manager with the settings from my `/.bash_profile or `/.bashrc files. To double check I open the run dialog, key the following command “echo $PATH”, hit the advanced button, set it to run the command in a console, and execute.
The tricky part is knowing which file to edit for your particular system. You might get away with editing /etc/X11/Xsession or you might need to find the Xsession that is executed, for example, under a Gnome based system. An alternative approach in KDE is to put a bash script in the env folder under ~/.kde. Scripts here get sourced on KDE startup and are much safer than editing files under /etc. That’s the tip for today. Show your love in the square below…