Understanding “Run Command as a Login Shell” option in GNOME Terminal

This post was made with another stylesheet and it might be messed up!

gnome-terminal-iconIf you open a GNOME Terminal and navigate @ Edit -> Profile Preferences -> Title and Command, there is a “Run Command as a Login Shell”option which is unchecked -at least on most GNOME Distros.

This can lead in issues that are hard to be tracked by new users that using Linux just to make their work, without want to learn how their system works -which makes perfect sense!


.bashrc Vs .bash_profile

In your $HOME you will find (depending the distros) two hidden files <.bashrc> and <.bash_profile> which are responsible for loading environments, starting -not GUI- programs, define functions, making alias etc, in your GNOME. The difference in quick between these two is that <.bashrc> is the place to put stuff that applies only for bash itself, where <.bash_profile> is the place to put stuff that applies to your whole session.

Many guides in popular applications seems to ignore in their examples/samples that in Linux Distros by default GNOME Terminal doesn’t act as a Login Shell; the examples don’t seem to work and users should Google around to find out what’s the issue.


Hello World Example

Let’s make a typical Hello World GJS Script and try to load it in our environment. In my $HOME:

$ mkdir .jsscripts
$ cd .jsscripts
$ gedit hello

Print a Hello World in Gnome JavaScript (GJS)

#!/usr/bin/gjs
print("hello world!");

Make it executable

$ chmod +x hello

Now I am gonna add this on $PATH through my <.bash_profile>.

$ gedit ~/.bash_profile

And add in the end of the file

export PATH=$PATH:$HOME/.jsscripts

I now open a new terminal and typing

$ hello

and I am expecting my script to be executed.

bash_helloworld

What happens is that I if we have “Run Command as a Login Shell” unchecked, Terminal won’t find our script while if we check that, it will normally run it. In this particular example we should had added the script in <.bashrc> and not in <.bash_profile>.

As a rule, system wide environment and startup programs, for login setup go in <.bash_profile>, and functions and aliases go in <.bashrc>.


A Real Example

Lets take as an example the very popular under Linux, RVM (Ruby Version Manager) installation. RVM gets installed under <~/.rvm/bin/rvm> and adds by default (in user-based installation) its environment in <.bash_profile>.

rvm-bash

As the above figure shows if we test our RVM installation

$ type rvm | head -n 1

It won’t work if we haven’t set Terminal to run commands as Login Shell. Instead we should source the script

$ source ~/.rvm/bin/rvm

that will work only for the current Terminal, or instead we can source it on our <.bashrc>, so it will work for all Terminal Sessions.

if [ -f ~/.bashrc ]; then
   . ~/.bashrc
fi

<.bash_profile> will be executed  for remote SSH and console logins  before the initial command prompt, but when you are opening a Terminal from within GNOME Session <.bashrc> is executed before the window command prompt.

I hope that helped, but as always you can get full documentation in The Linux Documentation Project.

                 

  • Kamil Páral

    That’s not really correct. ~/.bash_profile changes will get picked up the next you log in into your GNOME session. Then it will work also in GNOME Terminal. You don’t need to run it as a login shell. If you need the changes to apply immediately, you can simply do “source ~/.bash_profile”.

    • alex285

      That will work only in remotely ssh or in console. When you start GNOME session, .bashrc executed first, so you need to source it. What is your distro?

  • http://varemenos.com/ Adonis K. (Varemenos)

    Great article, thanks for explaining the difference!
    What im wondering though, do they both contain the same options?
    Could i replace my current .bash_profile with what i have in .bashrc? (my .bash_profile is empty right now)

    • alex285

      You can check the global profile and bashrc to understand what each file should contain.
      less /etc/profile
      less /etc/bashrc

      # System wide environment and startup programs, for login setup go in .bash_profile
      # Functions and aliases go in bashrc

      Obviously what is there, applies also for your account.

  • Philip Witte

    These kinds of articles are great! Thanks!

  • http://jgomo3.blogspot.com/ Jesús Gómez

    Finally an explanation. I think that option in Gnome Terminal is a “path” for those who don’t understand what a “login shell” means. Gnome Terminal should not behave like a “login shell” because normally user doesn’t authenticate. So the correct think to do is use the init files as they are intended to be used.

    What i don’t get right now is why Gnome Terminal doesn’t get the .bash_profile as it has nothing to do with being or not being a login shell.