17

If I examine my bash shell in an OS X (10.6.4) terminal, such as by typing echo $0, there is a dash in front of my shell, like this: -bash. What does this dash mean?

1 Answer 1

18

It means that bash is invoked as a login shell.


man bash says:

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

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.

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.


You'll find login -pf in your ps output, which starts a login shell by default.

Try running login -pf and login -pfl and see the difference. man login describes the difference:

-l Tells the program executed by login that this is not a login session (by convention, a login session is signalled to the program with a hyphen as the first character of argv[0]; this option disables that), and prevents it from chdir(2)ing to the user's home directory. The default is to add the hyphen (this is a login session).

9
  • Great answer, thanks for the explanation and man cites May 4, 2011 at 6:24
  • 1
    I am using ubuntu, and I see different behaviors of echo $0: if I type echo $0 in the terminal invoked by ctrl + alt + f1, the result is -bash; but if I type it in termianl invoked by ctrl + alt + t, result is bash(no leading dash). Could you explain why? Mar 12, 2014 at 11:09
  • @frozen-flame One is a login shell, the other isn't. Note that OS X Terminal and Linux terminals have different behavior in that regard.
    – Daniel Beck
    Mar 12, 2014 at 12:39
  • @DanielBeck that's not a proof that the other isn't a login shell. When bash is launched with --login argument, it doesn't change its argv[0], but does act as a login shell.
    – Ruslan
    Oct 12, 2016 at 11:42
  • 1
    @ynn man login mentions "The default is to add the hyphen (this is a login session)", which implies it's in addition to the regular name.
    – Daniel Beck
    Jun 26, 2020 at 7:10

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.