I am using Debian Stretch with Xfce. My ~/bin directory has a lot of scripts. This of course is in my $PATH, and I run scripts from there sometimes when I log in to Xfce via Session and Startup > Application Autostart. In my ~/.bashrc file I have my custom aliases and functions. I use the functions in the ~/.bashrc file for shorter bits of code that I would rather not create a whole script for in ~/bin. However, sometimes I would like to run a function from my ~/.bashrc file as a GUI login item.

I created a test function in ~/.bashrc. I then set that function to run from ~/.profile. The function runs successfully when I do the following: - Run bash -l from a terminal - Log in on tty session

However it does not run when I log in to my graphical Xfce environment from LightDM. The function does not run either when added in Session and Startup > Application Autostart in Xfce. To test further I added a command directly in ~/.profile. It does run when I log in to Xfce.

I am unable to run a ~/.bashrc function when logging into a graphical environment. Is this bug? Should a setting be enabled to run functions at GUI login? Or, is my system just designed this way? Thanks

  • What do you mean by "login item"? Do you just want to have the functions available in a login shell when you want them? Or do you want one of these functions to be run automatically when you log in?
    – terdon
    Feb 27 '17 at 22:08
  • I want it to run once when I log in. I could run it in my ~/.bashrc, but this runs it every time I open a terminal session.
    – jbrock
    Feb 27 '17 at 22:09
  • So you just want one specific function to i) run once each time you log in and ii) be available to run on demand in normal (non-login) shells? Why don't you just call the function from ~/.profile then?
    – terdon
    Feb 27 '17 at 22:11
  • I had tried to call it from ~/.profile, and it did not run for some reason.
    – jbrock
    Feb 27 '17 at 22:12
  • 1
    OK. Please edit your question and give us more details then. First, explain exactly what you want to happen (I'm afraid "I want to run it as a login item" isn't very understandable). Then, explain that you put it in ~/.profile and it failed. Also mention whether you need this to happen when you log in from the GUI or from the command line or both. Finally, explain why you don't just put that one function into a script and have the script run on login.
    – terdon
    Feb 27 '17 at 22:23

The default ~/.profile sources ~/.bashrc only if BASH_VERSION is non-empty, i.e. (from default .profile):

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"

Under Lightdm, startup scripts are executed by /bin/sh, which on Debian is a symlink to /bin/dash. This means that when ~/.profile is sourced at startup, BASH_VERSION is empty, and thus, ~/.bashrc does not get sourced.

Possible solutions

  1. Run the functions from ~/bin

  2. Another solution (if you need more than one at startup) is to have a separate file, say ~/.startup_functions where you keep all your functions, source the file from .profile and execute the functions from here. Notice that, since you are executing them with /bin/dash, you have to check for compatibility.

Otherwise, if you want to use bash as startup shell and source .bashrc at startup, one thing you can do is to set bash as default /bin/sh, i.e. run

$ dpkg-reconfigure dash 

and follow the instructions.

If you want to define the functions in .bashrc and run them at startup, notice that .profile loads .bashrc only if the shell is interactive, if (as by default) you have the following test in .bashrc

# If not running interactively, don't do anything
case $- in
    *i*) ;;
    *) return;;

which means you would have to either remove this test or define the functions you need before, anyway, I'd stick to running the function from ~/bin.

  • I could not quite get the ~/.bash_profile option or removing the $BASH_VERSION test to work in ~/.profile for the login ~/.bashrc function. Thank you though for the input. I'll plan on just using ~/bin then. That'll do just fine... ;)
    – jbrock
    Apr 30 '17 at 22:33
  • @jbrock my fault, this wouldn't even work. I played around a bit with it this weekend, and edited the answer to include more accurate info. But yes, your best option is to use ~/bin in my opinion.
    – resc
    May 1 '17 at 10:36
  • I tried every new step that you added. No luck. Commenting out that case statement actually made it where I could not log in on GUI at all. (I of course un-commented it in a tty.) I appreciate the steadfast and valiant effort though. I did learn a few things. No issue in running my functions from bin.
    – jbrock
    May 7 '17 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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