Difference between Login Shell and Non-Login Shell? Announcing the arrival of Valued Associate...

What does the "x" in "x86" represent?

Echoing a tail command produces unexpected output?

What is a non-alternating simple group with big order, but relatively few conjugacy classes?

Why did the rest of the Eastern Bloc not invade Yugoslavia?

Overriding an object in memory with placement new

Dating a Former Employee

Should I use a zero-interest credit card for a large one-time purchase?

How to answer "Have you ever been terminated?"

Should I discuss the type of campaign with my players?

How to deal with a team lead who never gives me credit?

English words in a non-english sci-fi novel

Identifying polygons that intersect with another layer using QGIS?

51k Euros annually for a family of 4 in Berlin: Is it enough?

Coloring maths inside a tcolorbox

The logistics of corpse disposal

String `!23` is replaced with `docker` in command line

Extract all GPU name, model and GPU ram

Can a non-EU citizen traveling with me come with me through the EU passport line?

What's the meaning of 間時肆拾貳 at a car parking sign

Are two submodules (where one is contained in the other) isomorphic if their quotientmodules are isomorphic?

Storing hydrofluoric acid before the invention of plastics

What is the role of the transistor and diode in a soft start circuit?

Apollo command module space walk?

How to find all the available tools in mac terminal?



Difference between Login Shell and Non-Login Shell?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionWhat is the difference between interactive shells, login shells, non-login shell and their use cases?login/non-login and interactive/non-interactive shellsdifference between bash, -bash and /bin/bash in ps outputDifferent representation of bash in 'ps -f' commandHow to make the command in /etc/profile be found?Why script name ($0) is different when logged in over ssh?The Shell initially Invoked After I Power On My Computer is Login or Non-login Shell?How is sudo set to not change $HOME in Ubuntu and how to disable this behavior?How to SSH into a specific directory?Why doesn't my ~/.bash_profile work?How to check if a shell is login/interactive/batchWhat is the difference between interactive shells, login shells, non-login shell and their use cases?How do I tell whether I'm in a non-login shell or a login shell?Are there non-interactive login shells?Is gnome terminal is a type of non login shell?WHY a **login** shell over a **non-login** shell?ssh command and non-interactive, non-login shellWhich file is loaded and executed by a non-interactive non-login shell?How to make sense of non-interactive login shell?Non-interactive & non-login shell environment?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







280















I understand the basic difference between an interactive shell and a non-interactive shell. But what exactly differentiates a login shell from a non-login shell?



Can you give examples for uses of a non-login interactive shell?










share|improve this question


















  • 40





    I think the question is better phrased as "Why do/should we care to differentiate login and non-login shells?" Many places on the web already tell us what are the differences, in terms of what startup files each read; but none of them seems to answer the "why" in a satisfactory and convincing way. Example use cases where you definitely do not want one or the other behaviour would be great.

    – Kal
    Apr 15 '13 at 3:49








  • 2





    @Kal This would have to be a different question, since no answer here actually covers that. Edit : Actually, here it is : WHY a login shell over a non-login shell?.

    – Skippy le Grand Gourou
    Aug 14 '18 at 13:15




















280















I understand the basic difference between an interactive shell and a non-interactive shell. But what exactly differentiates a login shell from a non-login shell?



Can you give examples for uses of a non-login interactive shell?










share|improve this question


















  • 40





    I think the question is better phrased as "Why do/should we care to differentiate login and non-login shells?" Many places on the web already tell us what are the differences, in terms of what startup files each read; but none of them seems to answer the "why" in a satisfactory and convincing way. Example use cases where you definitely do not want one or the other behaviour would be great.

    – Kal
    Apr 15 '13 at 3:49








  • 2





    @Kal This would have to be a different question, since no answer here actually covers that. Edit : Actually, here it is : WHY a login shell over a non-login shell?.

    – Skippy le Grand Gourou
    Aug 14 '18 at 13:15
















280












280








280


188






I understand the basic difference between an interactive shell and a non-interactive shell. But what exactly differentiates a login shell from a non-login shell?



Can you give examples for uses of a non-login interactive shell?










share|improve this question














I understand the basic difference between an interactive shell and a non-interactive shell. But what exactly differentiates a login shell from a non-login shell?



Can you give examples for uses of a non-login interactive shell?







shell login






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 8 '12 at 20:57









IgorioIgorio

2,01451511




2,01451511








  • 40





    I think the question is better phrased as "Why do/should we care to differentiate login and non-login shells?" Many places on the web already tell us what are the differences, in terms of what startup files each read; but none of them seems to answer the "why" in a satisfactory and convincing way. Example use cases where you definitely do not want one or the other behaviour would be great.

    – Kal
    Apr 15 '13 at 3:49








  • 2





    @Kal This would have to be a different question, since no answer here actually covers that. Edit : Actually, here it is : WHY a login shell over a non-login shell?.

    – Skippy le Grand Gourou
    Aug 14 '18 at 13:15
















  • 40





    I think the question is better phrased as "Why do/should we care to differentiate login and non-login shells?" Many places on the web already tell us what are the differences, in terms of what startup files each read; but none of them seems to answer the "why" in a satisfactory and convincing way. Example use cases where you definitely do not want one or the other behaviour would be great.

    – Kal
    Apr 15 '13 at 3:49








  • 2





    @Kal This would have to be a different question, since no answer here actually covers that. Edit : Actually, here it is : WHY a login shell over a non-login shell?.

    – Skippy le Grand Gourou
    Aug 14 '18 at 13:15










40




40





I think the question is better phrased as "Why do/should we care to differentiate login and non-login shells?" Many places on the web already tell us what are the differences, in terms of what startup files each read; but none of them seems to answer the "why" in a satisfactory and convincing way. Example use cases where you definitely do not want one or the other behaviour would be great.

– Kal
Apr 15 '13 at 3:49







I think the question is better phrased as "Why do/should we care to differentiate login and non-login shells?" Many places on the web already tell us what are the differences, in terms of what startup files each read; but none of them seems to answer the "why" in a satisfactory and convincing way. Example use cases where you definitely do not want one or the other behaviour would be great.

– Kal
Apr 15 '13 at 3:49






2




2





@Kal This would have to be a different question, since no answer here actually covers that. Edit : Actually, here it is : WHY a login shell over a non-login shell?.

– Skippy le Grand Gourou
Aug 14 '18 at 13:15







@Kal This would have to be a different question, since no answer here actually covers that. Edit : Actually, here it is : WHY a login shell over a non-login shell?.

– Skippy le Grand Gourou
Aug 14 '18 at 13:15












4 Answers
4






active

oldest

votes


















278














A login shell is the first process that executes under your user ID when you log in for an interactive session. The login process tells the shell to behave as a login shell with a convention: passing argument 0, which is normally the name of the shell executable, with a - character prepended (e.g. -bash whereas it would normally be bash. Login shells typically read a file that does things like setting environment variables: /etc/profile and ~/.profile for the traditional Bourne shell, ~/.bash_profile additionally for bash, /etc/zprofile and ~/.zprofile for zsh, /etc/csh.login and ~/.login for csh, etc.



When you log in on a text console, or through SSH, or with su -, you get an interactive login shell. When you log in in graphical mode (on an X display manager), you don't get a login shell, instead you get a session manager or a window manager.



It's rare to run a non-interactive login shell, but some X settings do that when you log in with a display manager, so as to arrange to read the profile files. Other settings (this depends on the distribution and on the display manager) read /etc/profile and ~/.profile explicitly, or don't read them. Another way to get a non-interactive login shell is to log in remotely with a command passed through standard input which is not a terminal, e.g. ssh example.com <my-script-which-is-stored-locally (as opposed to ssh example.com my-script-which-is-on-the-remote-machine, which runs a non-interactive, non-login shell).



When you start a shell in a terminal in an existing session (screen, X terminal, Emacs terminal buffer, a shell inside another, etc.), you get an interactive, non-login shell. That shell might read a shell configuration file (~/.bashrc for bash invoked as bash, /etc/zshrc and ~/.zshrc for zsh, /etc/csh.cshrc and ~/.cshrc for csh, the file indicated by the ENV variable for POSIX/XSI-compliant shells such as dash, ksh, and bash when invoked as sh, $ENV if set and ~/.mkshrc for mksh, etc.).



When a shell runs a script or a command passed on its command line, it's a non-interactive, non-login shell. Such shells run all the time: it's very common that when a program calls another program, it really runs a tiny script in a shell to invoke that other program. Some shells read a startup file in this case (bash runs the file indicated by the BASH_ENV variable, zsh runs /etc/zshenv and ~/.zshenv), but this is risky: the shell can be invoked in all sorts of contexts, and there's hardly anything you can do that might not break something.



I'm simplifying a little, see the manual for the gory details.






share|improve this answer





















  • 2





    Could you give example how to run bash as a non-interactive login shell?

    – Piotr Dobrogost
    Jun 16 '13 at 8:47






  • 12





    @PiotrDobrogost echo $- | bash -lx

    – Gilles
    Jun 16 '13 at 12:11






  • 1





    I don't know if this is true in general, but I want to note that when I open a new terminal (on osx using default settings), I get a login shell even though I never type in my username or password.

    – Kevin Wheeler
    Aug 28 '15 at 22:55






  • 3





    @KevinWheeler On OSX, by default, the Terminal application runs a login shell. (As I explain, the program that starts the shell decides whether the shell acts as a login shell.) That's not the normal way to do things.

    – Gilles
    Aug 28 '15 at 23:01






  • 2





    @IAmJulianAcosta If FOO is an environment variable (i.e. .profile contains export FOO=something) then it's available to all subprocesses, including foo.sh. If you change .profile to export FOO=something_else then ./foo.sh will still print something until the next time you log in.

    – Gilles
    Oct 6 '16 at 15:13



















39














To tell if you are in a login shell:



prompt> echo $0
-bash # "-" is the first character. Therefore, this is a login shell.

prompt> echo $0
bash # "-" is NOT the first character. This is NOT a login shell.


Information can be found in man bash (search for Invocation). Here is an excerpt:




A login shell is one whose first character of argument zero is a -, or
one started with the --login option.




You can test this yourself. Anytime you SSH, you are using a login shell. For Example:



prompt> ssh user@localhost
user@localhost's password:
prompt> echo $0
-bash


The importance of using a login shell is that any settings in /home/user/.bash_profile will get executed. Here is a little more information if you are interested (from man bash)




"When bash is invoked as an interactive login shell, or as a
non-interactive shell with the --login option, it first reads and
executes commands from the file /etc/profile, if that file exists.
After reading that file, it looks for ~/.bash_profile,
~/.bash_login, and ~/.profile, in that order, and reads and
executes commands from the first one that exists and is readable. The
--noprofile option may be used when the shell is started to inhibit this behavior."







share|improve this answer

































    23














    In a login shell, argv[0][0] == '-'.
    This is how it knows it's a login shell.



    And then in some situations it behaves differently depending on its "login shell" status. E.g. a shell, that is not a login shell, would not execute a "logout" command.






    share|improve this answer





















    • 4





      According to man bash, with emphasis added, "A login shell is one whose first character of argument zero is a -, or one started with the --login option."

      – Wildcard
      Jan 23 '17 at 11:49



















    18














    A shell started in a new terminal in a GUI would be an interactive non-login shell. It would source your .bashrc, but not your .profile, for example.






    share|improve this answer
























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f38175%2fdifference-between-login-shell-and-non-login-shell%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      278














      A login shell is the first process that executes under your user ID when you log in for an interactive session. The login process tells the shell to behave as a login shell with a convention: passing argument 0, which is normally the name of the shell executable, with a - character prepended (e.g. -bash whereas it would normally be bash. Login shells typically read a file that does things like setting environment variables: /etc/profile and ~/.profile for the traditional Bourne shell, ~/.bash_profile additionally for bash, /etc/zprofile and ~/.zprofile for zsh, /etc/csh.login and ~/.login for csh, etc.



      When you log in on a text console, or through SSH, or with su -, you get an interactive login shell. When you log in in graphical mode (on an X display manager), you don't get a login shell, instead you get a session manager or a window manager.



      It's rare to run a non-interactive login shell, but some X settings do that when you log in with a display manager, so as to arrange to read the profile files. Other settings (this depends on the distribution and on the display manager) read /etc/profile and ~/.profile explicitly, or don't read them. Another way to get a non-interactive login shell is to log in remotely with a command passed through standard input which is not a terminal, e.g. ssh example.com <my-script-which-is-stored-locally (as opposed to ssh example.com my-script-which-is-on-the-remote-machine, which runs a non-interactive, non-login shell).



      When you start a shell in a terminal in an existing session (screen, X terminal, Emacs terminal buffer, a shell inside another, etc.), you get an interactive, non-login shell. That shell might read a shell configuration file (~/.bashrc for bash invoked as bash, /etc/zshrc and ~/.zshrc for zsh, /etc/csh.cshrc and ~/.cshrc for csh, the file indicated by the ENV variable for POSIX/XSI-compliant shells such as dash, ksh, and bash when invoked as sh, $ENV if set and ~/.mkshrc for mksh, etc.).



      When a shell runs a script or a command passed on its command line, it's a non-interactive, non-login shell. Such shells run all the time: it's very common that when a program calls another program, it really runs a tiny script in a shell to invoke that other program. Some shells read a startup file in this case (bash runs the file indicated by the BASH_ENV variable, zsh runs /etc/zshenv and ~/.zshenv), but this is risky: the shell can be invoked in all sorts of contexts, and there's hardly anything you can do that might not break something.



      I'm simplifying a little, see the manual for the gory details.






      share|improve this answer





















      • 2





        Could you give example how to run bash as a non-interactive login shell?

        – Piotr Dobrogost
        Jun 16 '13 at 8:47






      • 12





        @PiotrDobrogost echo $- | bash -lx

        – Gilles
        Jun 16 '13 at 12:11






      • 1





        I don't know if this is true in general, but I want to note that when I open a new terminal (on osx using default settings), I get a login shell even though I never type in my username or password.

        – Kevin Wheeler
        Aug 28 '15 at 22:55






      • 3





        @KevinWheeler On OSX, by default, the Terminal application runs a login shell. (As I explain, the program that starts the shell decides whether the shell acts as a login shell.) That's not the normal way to do things.

        – Gilles
        Aug 28 '15 at 23:01






      • 2





        @IAmJulianAcosta If FOO is an environment variable (i.e. .profile contains export FOO=something) then it's available to all subprocesses, including foo.sh. If you change .profile to export FOO=something_else then ./foo.sh will still print something until the next time you log in.

        – Gilles
        Oct 6 '16 at 15:13
















      278














      A login shell is the first process that executes under your user ID when you log in for an interactive session. The login process tells the shell to behave as a login shell with a convention: passing argument 0, which is normally the name of the shell executable, with a - character prepended (e.g. -bash whereas it would normally be bash. Login shells typically read a file that does things like setting environment variables: /etc/profile and ~/.profile for the traditional Bourne shell, ~/.bash_profile additionally for bash, /etc/zprofile and ~/.zprofile for zsh, /etc/csh.login and ~/.login for csh, etc.



      When you log in on a text console, or through SSH, or with su -, you get an interactive login shell. When you log in in graphical mode (on an X display manager), you don't get a login shell, instead you get a session manager or a window manager.



      It's rare to run a non-interactive login shell, but some X settings do that when you log in with a display manager, so as to arrange to read the profile files. Other settings (this depends on the distribution and on the display manager) read /etc/profile and ~/.profile explicitly, or don't read them. Another way to get a non-interactive login shell is to log in remotely with a command passed through standard input which is not a terminal, e.g. ssh example.com <my-script-which-is-stored-locally (as opposed to ssh example.com my-script-which-is-on-the-remote-machine, which runs a non-interactive, non-login shell).



      When you start a shell in a terminal in an existing session (screen, X terminal, Emacs terminal buffer, a shell inside another, etc.), you get an interactive, non-login shell. That shell might read a shell configuration file (~/.bashrc for bash invoked as bash, /etc/zshrc and ~/.zshrc for zsh, /etc/csh.cshrc and ~/.cshrc for csh, the file indicated by the ENV variable for POSIX/XSI-compliant shells such as dash, ksh, and bash when invoked as sh, $ENV if set and ~/.mkshrc for mksh, etc.).



      When a shell runs a script or a command passed on its command line, it's a non-interactive, non-login shell. Such shells run all the time: it's very common that when a program calls another program, it really runs a tiny script in a shell to invoke that other program. Some shells read a startup file in this case (bash runs the file indicated by the BASH_ENV variable, zsh runs /etc/zshenv and ~/.zshenv), but this is risky: the shell can be invoked in all sorts of contexts, and there's hardly anything you can do that might not break something.



      I'm simplifying a little, see the manual for the gory details.






      share|improve this answer





















      • 2





        Could you give example how to run bash as a non-interactive login shell?

        – Piotr Dobrogost
        Jun 16 '13 at 8:47






      • 12





        @PiotrDobrogost echo $- | bash -lx

        – Gilles
        Jun 16 '13 at 12:11






      • 1





        I don't know if this is true in general, but I want to note that when I open a new terminal (on osx using default settings), I get a login shell even though I never type in my username or password.

        – Kevin Wheeler
        Aug 28 '15 at 22:55






      • 3





        @KevinWheeler On OSX, by default, the Terminal application runs a login shell. (As I explain, the program that starts the shell decides whether the shell acts as a login shell.) That's not the normal way to do things.

        – Gilles
        Aug 28 '15 at 23:01






      • 2





        @IAmJulianAcosta If FOO is an environment variable (i.e. .profile contains export FOO=something) then it's available to all subprocesses, including foo.sh. If you change .profile to export FOO=something_else then ./foo.sh will still print something until the next time you log in.

        – Gilles
        Oct 6 '16 at 15:13














      278












      278








      278







      A login shell is the first process that executes under your user ID when you log in for an interactive session. The login process tells the shell to behave as a login shell with a convention: passing argument 0, which is normally the name of the shell executable, with a - character prepended (e.g. -bash whereas it would normally be bash. Login shells typically read a file that does things like setting environment variables: /etc/profile and ~/.profile for the traditional Bourne shell, ~/.bash_profile additionally for bash, /etc/zprofile and ~/.zprofile for zsh, /etc/csh.login and ~/.login for csh, etc.



      When you log in on a text console, or through SSH, or with su -, you get an interactive login shell. When you log in in graphical mode (on an X display manager), you don't get a login shell, instead you get a session manager or a window manager.



      It's rare to run a non-interactive login shell, but some X settings do that when you log in with a display manager, so as to arrange to read the profile files. Other settings (this depends on the distribution and on the display manager) read /etc/profile and ~/.profile explicitly, or don't read them. Another way to get a non-interactive login shell is to log in remotely with a command passed through standard input which is not a terminal, e.g. ssh example.com <my-script-which-is-stored-locally (as opposed to ssh example.com my-script-which-is-on-the-remote-machine, which runs a non-interactive, non-login shell).



      When you start a shell in a terminal in an existing session (screen, X terminal, Emacs terminal buffer, a shell inside another, etc.), you get an interactive, non-login shell. That shell might read a shell configuration file (~/.bashrc for bash invoked as bash, /etc/zshrc and ~/.zshrc for zsh, /etc/csh.cshrc and ~/.cshrc for csh, the file indicated by the ENV variable for POSIX/XSI-compliant shells such as dash, ksh, and bash when invoked as sh, $ENV if set and ~/.mkshrc for mksh, etc.).



      When a shell runs a script or a command passed on its command line, it's a non-interactive, non-login shell. Such shells run all the time: it's very common that when a program calls another program, it really runs a tiny script in a shell to invoke that other program. Some shells read a startup file in this case (bash runs the file indicated by the BASH_ENV variable, zsh runs /etc/zshenv and ~/.zshenv), but this is risky: the shell can be invoked in all sorts of contexts, and there's hardly anything you can do that might not break something.



      I'm simplifying a little, see the manual for the gory details.






      share|improve this answer















      A login shell is the first process that executes under your user ID when you log in for an interactive session. The login process tells the shell to behave as a login shell with a convention: passing argument 0, which is normally the name of the shell executable, with a - character prepended (e.g. -bash whereas it would normally be bash. Login shells typically read a file that does things like setting environment variables: /etc/profile and ~/.profile for the traditional Bourne shell, ~/.bash_profile additionally for bash, /etc/zprofile and ~/.zprofile for zsh, /etc/csh.login and ~/.login for csh, etc.



      When you log in on a text console, or through SSH, or with su -, you get an interactive login shell. When you log in in graphical mode (on an X display manager), you don't get a login shell, instead you get a session manager or a window manager.



      It's rare to run a non-interactive login shell, but some X settings do that when you log in with a display manager, so as to arrange to read the profile files. Other settings (this depends on the distribution and on the display manager) read /etc/profile and ~/.profile explicitly, or don't read them. Another way to get a non-interactive login shell is to log in remotely with a command passed through standard input which is not a terminal, e.g. ssh example.com <my-script-which-is-stored-locally (as opposed to ssh example.com my-script-which-is-on-the-remote-machine, which runs a non-interactive, non-login shell).



      When you start a shell in a terminal in an existing session (screen, X terminal, Emacs terminal buffer, a shell inside another, etc.), you get an interactive, non-login shell. That shell might read a shell configuration file (~/.bashrc for bash invoked as bash, /etc/zshrc and ~/.zshrc for zsh, /etc/csh.cshrc and ~/.cshrc for csh, the file indicated by the ENV variable for POSIX/XSI-compliant shells such as dash, ksh, and bash when invoked as sh, $ENV if set and ~/.mkshrc for mksh, etc.).



      When a shell runs a script or a command passed on its command line, it's a non-interactive, non-login shell. Such shells run all the time: it's very common that when a program calls another program, it really runs a tiny script in a shell to invoke that other program. Some shells read a startup file in this case (bash runs the file indicated by the BASH_ENV variable, zsh runs /etc/zshenv and ~/.zshenv), but this is risky: the shell can be invoked in all sorts of contexts, and there's hardly anything you can do that might not break something.



      I'm simplifying a little, see the manual for the gory details.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Feb 3 '17 at 12:17

























      answered Sep 1 '12 at 2:07









      GillesGilles

      548k13011131631




      548k13011131631








      • 2





        Could you give example how to run bash as a non-interactive login shell?

        – Piotr Dobrogost
        Jun 16 '13 at 8:47






      • 12





        @PiotrDobrogost echo $- | bash -lx

        – Gilles
        Jun 16 '13 at 12:11






      • 1





        I don't know if this is true in general, but I want to note that when I open a new terminal (on osx using default settings), I get a login shell even though I never type in my username or password.

        – Kevin Wheeler
        Aug 28 '15 at 22:55






      • 3





        @KevinWheeler On OSX, by default, the Terminal application runs a login shell. (As I explain, the program that starts the shell decides whether the shell acts as a login shell.) That's not the normal way to do things.

        – Gilles
        Aug 28 '15 at 23:01






      • 2





        @IAmJulianAcosta If FOO is an environment variable (i.e. .profile contains export FOO=something) then it's available to all subprocesses, including foo.sh. If you change .profile to export FOO=something_else then ./foo.sh will still print something until the next time you log in.

        – Gilles
        Oct 6 '16 at 15:13














      • 2





        Could you give example how to run bash as a non-interactive login shell?

        – Piotr Dobrogost
        Jun 16 '13 at 8:47






      • 12





        @PiotrDobrogost echo $- | bash -lx

        – Gilles
        Jun 16 '13 at 12:11






      • 1





        I don't know if this is true in general, but I want to note that when I open a new terminal (on osx using default settings), I get a login shell even though I never type in my username or password.

        – Kevin Wheeler
        Aug 28 '15 at 22:55






      • 3





        @KevinWheeler On OSX, by default, the Terminal application runs a login shell. (As I explain, the program that starts the shell decides whether the shell acts as a login shell.) That's not the normal way to do things.

        – Gilles
        Aug 28 '15 at 23:01






      • 2





        @IAmJulianAcosta If FOO is an environment variable (i.e. .profile contains export FOO=something) then it's available to all subprocesses, including foo.sh. If you change .profile to export FOO=something_else then ./foo.sh will still print something until the next time you log in.

        – Gilles
        Oct 6 '16 at 15:13








      2




      2





      Could you give example how to run bash as a non-interactive login shell?

      – Piotr Dobrogost
      Jun 16 '13 at 8:47





      Could you give example how to run bash as a non-interactive login shell?

      – Piotr Dobrogost
      Jun 16 '13 at 8:47




      12




      12





      @PiotrDobrogost echo $- | bash -lx

      – Gilles
      Jun 16 '13 at 12:11





      @PiotrDobrogost echo $- | bash -lx

      – Gilles
      Jun 16 '13 at 12:11




      1




      1





      I don't know if this is true in general, but I want to note that when I open a new terminal (on osx using default settings), I get a login shell even though I never type in my username or password.

      – Kevin Wheeler
      Aug 28 '15 at 22:55





      I don't know if this is true in general, but I want to note that when I open a new terminal (on osx using default settings), I get a login shell even though I never type in my username or password.

      – Kevin Wheeler
      Aug 28 '15 at 22:55




      3




      3





      @KevinWheeler On OSX, by default, the Terminal application runs a login shell. (As I explain, the program that starts the shell decides whether the shell acts as a login shell.) That's not the normal way to do things.

      – Gilles
      Aug 28 '15 at 23:01





      @KevinWheeler On OSX, by default, the Terminal application runs a login shell. (As I explain, the program that starts the shell decides whether the shell acts as a login shell.) That's not the normal way to do things.

      – Gilles
      Aug 28 '15 at 23:01




      2




      2





      @IAmJulianAcosta If FOO is an environment variable (i.e. .profile contains export FOO=something) then it's available to all subprocesses, including foo.sh. If you change .profile to export FOO=something_else then ./foo.sh will still print something until the next time you log in.

      – Gilles
      Oct 6 '16 at 15:13





      @IAmJulianAcosta If FOO is an environment variable (i.e. .profile contains export FOO=something) then it's available to all subprocesses, including foo.sh. If you change .profile to export FOO=something_else then ./foo.sh will still print something until the next time you log in.

      – Gilles
      Oct 6 '16 at 15:13













      39














      To tell if you are in a login shell:



      prompt> echo $0
      -bash # "-" is the first character. Therefore, this is a login shell.

      prompt> echo $0
      bash # "-" is NOT the first character. This is NOT a login shell.


      Information can be found in man bash (search for Invocation). Here is an excerpt:




      A login shell is one whose first character of argument zero is a -, or
      one started with the --login option.




      You can test this yourself. Anytime you SSH, you are using a login shell. For Example:



      prompt> ssh user@localhost
      user@localhost's password:
      prompt> echo $0
      -bash


      The importance of using a login shell is that any settings in /home/user/.bash_profile will get executed. Here is a little more information if you are interested (from man bash)




      "When bash is invoked as an interactive login shell, or as a
      non-interactive shell with the --login option, it first reads and
      executes commands from the file /etc/profile, if that file exists.
      After reading that file, it looks for ~/.bash_profile,
      ~/.bash_login, and ~/.profile, in that order, and reads and
      executes commands from the first one that exists and is readable. The
      --noprofile option may be used when the shell is started to inhibit this behavior."







      share|improve this answer






























        39














        To tell if you are in a login shell:



        prompt> echo $0
        -bash # "-" is the first character. Therefore, this is a login shell.

        prompt> echo $0
        bash # "-" is NOT the first character. This is NOT a login shell.


        Information can be found in man bash (search for Invocation). Here is an excerpt:




        A login shell is one whose first character of argument zero is a -, or
        one started with the --login option.




        You can test this yourself. Anytime you SSH, you are using a login shell. For Example:



        prompt> ssh user@localhost
        user@localhost's password:
        prompt> echo $0
        -bash


        The importance of using a login shell is that any settings in /home/user/.bash_profile will get executed. Here is a little more information if you are interested (from man bash)




        "When bash is invoked as an interactive login shell, or as a
        non-interactive shell with the --login option, it first reads and
        executes commands from the file /etc/profile, if that file exists.
        After reading that file, it looks for ~/.bash_profile,
        ~/.bash_login, and ~/.profile, in that order, and reads and
        executes commands from the first one that exists and is readable. The
        --noprofile option may be used when the shell is started to inhibit this behavior."







        share|improve this answer




























          39












          39








          39







          To tell if you are in a login shell:



          prompt> echo $0
          -bash # "-" is the first character. Therefore, this is a login shell.

          prompt> echo $0
          bash # "-" is NOT the first character. This is NOT a login shell.


          Information can be found in man bash (search for Invocation). Here is an excerpt:




          A login shell is one whose first character of argument zero is a -, or
          one started with the --login option.




          You can test this yourself. Anytime you SSH, you are using a login shell. For Example:



          prompt> ssh user@localhost
          user@localhost's password:
          prompt> echo $0
          -bash


          The importance of using a login shell is that any settings in /home/user/.bash_profile will get executed. Here is a little more information if you are interested (from man bash)




          "When bash is invoked as an interactive login shell, or as a
          non-interactive shell with the --login option, it first reads and
          executes commands from the file /etc/profile, if that file exists.
          After reading that file, it looks for ~/.bash_profile,
          ~/.bash_login, and ~/.profile, in that order, and reads and
          executes commands from the first one that exists and is readable. The
          --noprofile option may be used when the shell is started to inhibit this behavior."







          share|improve this answer















          To tell if you are in a login shell:



          prompt> echo $0
          -bash # "-" is the first character. Therefore, this is a login shell.

          prompt> echo $0
          bash # "-" is NOT the first character. This is NOT a login shell.


          Information can be found in man bash (search for Invocation). Here is an excerpt:




          A login shell is one whose first character of argument zero is a -, or
          one started with the --login option.




          You can test this yourself. Anytime you SSH, you are using a login shell. For Example:



          prompt> ssh user@localhost
          user@localhost's password:
          prompt> echo $0
          -bash


          The importance of using a login shell is that any settings in /home/user/.bash_profile will get executed. Here is a little more information if you are interested (from man bash)




          "When bash is invoked as an interactive login shell, or as a
          non-interactive shell with the --login option, it first reads and
          executes commands from the file /etc/profile, if that file exists.
          After reading that file, it looks for ~/.bash_profile,
          ~/.bash_login, and ~/.profile, in that order, and reads and
          executes commands from the first one that exists and is readable. The
          --noprofile option may be used when the shell is started to inhibit this behavior."








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 5 hours ago

























          answered Oct 21 '15 at 14:46









          Timothy PulliamTimothy Pulliam

          1,4251025




          1,4251025























              23














              In a login shell, argv[0][0] == '-'.
              This is how it knows it's a login shell.



              And then in some situations it behaves differently depending on its "login shell" status. E.g. a shell, that is not a login shell, would not execute a "logout" command.






              share|improve this answer





















              • 4





                According to man bash, with emphasis added, "A login shell is one whose first character of argument zero is a -, or one started with the --login option."

                – Wildcard
                Jan 23 '17 at 11:49
















              23














              In a login shell, argv[0][0] == '-'.
              This is how it knows it's a login shell.



              And then in some situations it behaves differently depending on its "login shell" status. E.g. a shell, that is not a login shell, would not execute a "logout" command.






              share|improve this answer





















              • 4





                According to man bash, with emphasis added, "A login shell is one whose first character of argument zero is a -, or one started with the --login option."

                – Wildcard
                Jan 23 '17 at 11:49














              23












              23








              23







              In a login shell, argv[0][0] == '-'.
              This is how it knows it's a login shell.



              And then in some situations it behaves differently depending on its "login shell" status. E.g. a shell, that is not a login shell, would not execute a "logout" command.






              share|improve this answer















              In a login shell, argv[0][0] == '-'.
              This is how it knows it's a login shell.



              And then in some situations it behaves differently depending on its "login shell" status. E.g. a shell, that is not a login shell, would not execute a "logout" command.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 9 '12 at 4:52









              Mat

              40.1k8124128




              40.1k8124128










              answered May 8 '12 at 22:57









              BOPOHOKBOPOHOK

              23112




              23112








              • 4





                According to man bash, with emphasis added, "A login shell is one whose first character of argument zero is a -, or one started with the --login option."

                – Wildcard
                Jan 23 '17 at 11:49














              • 4





                According to man bash, with emphasis added, "A login shell is one whose first character of argument zero is a -, or one started with the --login option."

                – Wildcard
                Jan 23 '17 at 11:49








              4




              4





              According to man bash, with emphasis added, "A login shell is one whose first character of argument zero is a -, or one started with the --login option."

              – Wildcard
              Jan 23 '17 at 11:49





              According to man bash, with emphasis added, "A login shell is one whose first character of argument zero is a -, or one started with the --login option."

              – Wildcard
              Jan 23 '17 at 11:49











              18














              A shell started in a new terminal in a GUI would be an interactive non-login shell. It would source your .bashrc, but not your .profile, for example.






              share|improve this answer




























                18














                A shell started in a new terminal in a GUI would be an interactive non-login shell. It would source your .bashrc, but not your .profile, for example.






                share|improve this answer


























                  18












                  18








                  18







                  A shell started in a new terminal in a GUI would be an interactive non-login shell. It would source your .bashrc, but not your .profile, for example.






                  share|improve this answer













                  A shell started in a new terminal in a GUI would be an interactive non-login shell. It would source your .bashrc, but not your .profile, for example.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 8 '12 at 22:20









                  JulianJulian

                  46925




                  46925






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f38175%2fdifference-between-login-shell-and-non-login-shell%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

                      The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

                      Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...