30

When you enter an invalid command at a bash prompt you get the message

-bash: {command}: command not found

What does the - at the very beginning signify?

2
  • How exactly are you running the command? I tried to replicate, but I'm not getting the same results - e.g.> jhgjbjbkjln: command not found - no -bash: in there. Are you using bash in Unix, Linux, OSX, ...? I ask because maybe this is something a specific implementation put before the error message by default, and it doesn't mean anything... maybe.
    – jimm-cl
    Commented Aug 8, 2014 at 5:44
  • @jim Try ssh computername and your not count command to replicate .
    – Bernhard
    Commented Aug 8, 2014 at 5:48

3 Answers 3

30

It means that it is a login shell.

From man bash:

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

(In bash terminology, the "zeroth" argument is the command name which, in your case, was bash.) bash uses this as a signal to do login activities such as executing .bash_profile, etc.

One way that the dash may be added automatically is if the shell is started with exec. From the Bash manual:

exec [-cl] [-a name] [command [arguments]]

[...] If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command.

Example

Compare these two attempts to run the command nonexistent. First without -l:

$ exec bash
$ nonexistent
bash: nonexistent: command not found

And, second, with:

$ exec -l bash
$ nonexistent
-bash: nonexistent: command not found
3
  • 3
    I think the part of the documentation you're quoting doesn't match up with your explanation. The fact that exec -l prepends a dash is a possible way that may cause bash to be run as -bash, but that by itself doesn't say that it is a login shell, and there is a very good chance that the OP hasn't used exec -l at all. The part of the documentation that says -bash makes it a login shell is "INVOCATION A login shell is one whose first character of argument zero is a -, or one started with the --login option."
    – hvd
    Commented Aug 8, 2014 at 11:05
  • @hvd, agreed. Though correct, this answer misses the point. Commented Aug 8, 2014 at 12:35
  • 1
    "The zeroth argument" is not bash terminology, but Unix convention + C indexing: Each program gets passed a parameter list, whose initial entry is the program name, followed by the arguments. Since C arrays are indexed starting with zero, argument zero (that is, the zeroth argument) is the program name, and the actual arguments start with index 1.
    – celtschk
    Commented Aug 8, 2014 at 12:46
15

The other answer is fine as far as it goes, but it's worth mentioning that the feature is more general than bash.

Since ancient times, the login program has prepended a dash to argv[0] when it executes the user's shell, and the shell has recognized this as a sign that it should behave as a "login shell". It is documented in the V7 man pages here: login(1), sh(1).

All programs that provide a login-like service (authenticate a user and run a shell) should follow the "prepend dash" rule. For example, sshd does as you can see in ssh/session.c under this comment:

/*
 * If we have no command, execute the shell.  In this case, the shell
 * name to be passed in argv[0] is preceded by '-' to indicate that
 * this is a login shell.
 */

All shells recognize the leading dash. The equivalent -l option doesn't exist in the classic Bourne shell or original csh, but most newer shells (bash, dash, ksh, yash, tcsh, zsh, rc, es, fish and any semi-recent version of csh) have it.

1
  • 2
    Even though most shells now have the -l option, login still doesn't use it. All shells are expected to recognize the prefix on argv[0] as the "official" mechanism.
    – Barmar
    Commented Aug 13, 2014 at 20:15
0

The arguments of the function main is manually set by the parent process rather than preset by kernel. The parent process can set any value as the child's argv[0]. See man 3 exec

e.g. The parent can do

execlp("bash", "-bash");

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .