Set the $PATH for the Run (Alt+F2) Dialog


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:
fortune
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:
#!/bin/sh
I introduce the login parameter:
#!/bin/sh -l
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…

11 Comments

  1. Use more paragraphs you idiot.

  2. I’m not sure how adding extra paragraphs would affect my PATH setting but I guess it’s worth a try. This idiot thanks you for the helpful tip!

  3. Bernard

    Great tip! I just switched to Gnome from Xfce-4 (which doesn’t have this issue, at least on Gentoo), and this particular problem has been bugging me ever since! (Well, it’s only been 2 days, but still…)

  4. Yeah I keep the tip here because I’ve switched distros a number of times and while some get it right (earlier versions of Mepis) many of them (Ubuntu and derivatives) leave me scratching my head trying to recall how to resolve the problem.

  5. Ajith C

    Good naration – I rarely get fun reading tech stuff. Most of the IT related readings are dull & dry :(

    None of the above steps helped me in Ubuntu 9.04/GNOME

    Later I found .gnomerc file in $HOME, adding the PATH to .gnomerc helped

    # To add $HOME/bin to gnome’s PATH list (eg: run dialog)
    echo $PATH | grep -q -e “$HOME/bin”
    if [ $? != 0 ]
    then
    export PATH=$HOME/bin:$HOME/apps/bin:$PATH
    fi

    • Lorenzo

      A really funny answer,
      thanks all

  6. I don’t use the Gnome desktop though a buddy of mine had similar problems. It’s interesting that you found gnomerc because I believe there was a KDE equivalent that I found in either Mepis or Ubuntu some time ago. (~/kde/startup or something like that.) The problem was that it didn’t port between distros even distros running KDE. The core solution is actually two parts. 1st figure out which scripts your desktop manager (or window manager) runs on startup. This is the thing that varies between distros. The other part is remembering the -l or -login parameter. That’s why I blogged this so I can come back to it each time I install a distro.

  7. Да таков наш современный мир и боюсь наверное с этим ни чего не поделать:)

  8. James

    Handy info. I thought U had solved this on one of my boxen, but then could not do the same on another, so having an explanation is epic goodness.

  9. koladi

    Hi guy,
    thanks for making me laugh out loud when reading your story,.,.;)
    seems you ‘ve been in my shoes for quite a time (or vice versa…) when doing those things to your linux system…:)
    maybe you should write a book, or something?? Funny AND helpful IT stuff is rare.. just reading the Skillz function on top of your blog could convince me to buy it…

    By the way, this code in my .gnomerc did it for me (Ubuntu 11.10 with gnome3):

    #!/bin/bash

    if [ -d "$HOME/bin" ] ; then
    PATH=”$HOME/bin:$PATH”
    fi

    best regards…

  10. Hi Koladi! I’m so lazy these days! I’ve been meaning to update the Skillz method because it has obvious errors from when I 1st learned Obj-C and the embarrassing misspelling of Emperor that I intent to leave as my signature trademark. One of these days I’m going to put this blog to some good use.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 250 other followers

%d bloggers like this: