Interesting $0 Problem

Arthur Chance freebsd at
Fri Oct 28 08:45:29 UTC 2016

On 28/10/2016 02:30, Tim Daneliuk wrote:
> I was fidding with some shell code today and discovered it was breaking
> because $0 was returning "-/usr/local/bin/bash".   Why is there a leading
> dash here?  I've not seen that before.

This is from "man sh" in the "Invocation" section but presumably bash is
mimicking the standard behaviour.

> When first starting, the shell inspects argument 0, and if it begins
> with a dash (‘-’), the shell is also considered a login shell.  This
> is normally done automatically by the system when the user first
> logs in.

And in the source code for /usr/bin/login

         * Login shells have a leading '-' in front of argv[0]

Prepending a dash to a login shell has been standard behaviour since the
BSD days at least. I think it was in version 6 of the original Bell Labs
Unix as well, but after three and a half decades my memories for such
details are a bit hazy. Anyway, it's a standard marker.

Schrödinger's cat had 18 half lives.

More information about the freebsd-questions mailing list