Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how can i test if a bash shell is a login shell?

216 views
Skip to first unread message

Ken Pizzini

unread,
Oct 18, 1999, 3:00:00 AM10/18/99
to
On 17 Oct 1999 10:24:35 +0200, Franz Zahaurek <f...@gams.at> wrote:
>Matt Swift <sw...@alum.mit.edu> writes:
>
>> bash itself knows if it's a login shell or not because the behavior of
>> the builtin `logout' acts differently. how can the user determine if
>> a shell is a login shell (without executing `logout'...)?
>
>Take a look at man bash. Topic INVOCATION.

That mentions that $- will contain an "i" for interactive
shells, but there is no flag stored to differentiate a login
from a non-login interactive shell.

As to the original question, there is no direct way to determine this.

You can set "ignoreeof=t" and inspect the text of the error message
returned when you send an EOF to the shell.

You can contrive to set a variable to one value in your .bashrc and a
different value in your .bash_profile (being sure, if you source your
.bashrc within your .bash_profile, that the .bash_profile setting
happens after the .bashrc setting), and then just inspect the value
of that variable.

Finally, in addition to the unatractive "try ``logout''", there's
also the unattractive option of executing "suspend" --- without the
"-f" flag a "suspend" call will fail for interactive shells.

--Ken Pizzini

dbt

unread,
Oct 20, 1999, 3:00:00 AM10/20/99
to
Ken Pizzini <k...@halcyon.com> says:
>On 17 Oct 1999 10:24:35 +0200, Franz Zahaurek <f...@gams.at> wrote:
>>Matt Swift <sw...@alum.mit.edu> writes:
>>
>>> bash itself knows if it's a login shell or not because the behavior of
>>> the builtin `logout' acts differently. how can the user determine if
>>> a shell is a login shell (without executing `logout'...)?
>>
>>Take a look at man bash. Topic INVOCATION.
>
>That mentions that $- will contain an "i" for interactive
>shells, but there is no flag stored to differentiate a login
>from a non-login interactive shell.
>
>As to the original question, there is no direct way to determine this.

Here's a hint. Check $0.

--
David Terrell | d...@meat.net
Prime Minister, Nebcorp Inc | http://wwn.nebcorp.com/

Ken Pizzini

unread,
Oct 20, 1999, 3:00:00 AM10/20/99
to
On 20 Oct 1999 10:30:40 -0700, dbt <d...@meat.net> wrote:

>Ken Pizzini <k...@halcyon.com> says:
>>>Matt Swift <sw...@alum.mit.edu> writes:
>>>
>>>> bash itself knows if it's a login shell or not because the behavior of
>>>> the builtin `logout' acts differently. how can the user determine if
>>>> a shell is a login shell (without executing `logout'...)?
...

>>As to the original question, there is no direct way to determine this.
>
>Here's a hint. Check $0.

Good point, but it still isn't perfect:
$ grep foo: /etc/passwd
foo:P.ZDXJWlFIlgY:666:666:First Omnipotent Oneness:/home/foo:
$ cat ~foo/.profile
exec bash --login
$ echo $0
bash
$

I'd still consider that to be a login shell, despite the lack
of a leading "-" in the $0. On the other hand, the "look at $0"
approach is probably "good enough" for many (if not most) purposes,
so it's good that it was mentioned.

--Ken Pizzini

Matt Swift

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
>>"dbt" == dbt <d...@meat.net> writes:

dbt> Ken Pizzini <k...@halcyon.com> says:
>> On 17 Oct 1999 10:24:35 +0200, Franz Zahaurek <f...@gams.at>
>> wrote:

>>Matt Swift <sw...@alum.mit.edu> writes:
>>
>>> bash itself knows if it's a login shell or not because the
>> behavior of
>>> the builtin `logout' acts differently. how can the user determine
>> if
>>> a shell is a login shell (without executing `logout'...)?
>>

>>Take a look at man bash. Topic INVOCATION.
>>
>> That mentions that $- will contain an "i" for interactive
>> shells, but there is no flag stored to differentiate a login
>> from a non-login interactive shell.
>>

>> As to the original question, there is no direct way to
>> determine this.

dbt> Here's a hint. Check $0.

You get second place in the snotty
answer-that-doesn't-answer-the-question category.

Will anyone deign to actually spell out the answer, if one exists, to
poor ignorant me?


/tmp> echo $LOGINSHELL

/tmp> exec -c bash
/tmp> echo $0
bash
/tmp> echo $LOGINSHELL

/tmp> exec -c bash --login
/tmp> echo $0
bash
/tmp> echo $LOGINSHELL
true
/tmp> cat ~/.bash_profile
# This gets read by bash interactive login shells.

LOGINSHELL=true
source ~/.bashrc
/tmp>

Franz Zahaurek

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
Matt Swift <sw...@alum.mit.edu> writes:

[...]


> You get second place in the snotty
> answer-that-doesn't-answer-the-question category.
>
> Will anyone deign to actually spell out the answer, if one exists, to
> poor ignorant me?

Sorry for my short answer that I gave recently. Man bash says:

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

According to this you have to do 2 tests:

argument zero: firstchar=`expr $0 : '\(-\)*'`
-login flag: grep -- -login /proc/$$/cmdline

This second test only works with LINUX and the /proc-Filesystem.
With other systems you could look at the output of ps.

> /tmp> echo $LOGINSHELL
>
> /tmp> exec -c bash
> /tmp> echo $0
> bash
> /tmp> echo $LOGINSHELL
>
> /tmp> exec -c bash --login
> /tmp> echo $0
> bash
> /tmp> echo $LOGINSHELL
> true
> /tmp> cat ~/.bash_profile
> # This gets read by bash interactive login shells.
>
> LOGINSHELL=true
> source ~/.bashrc
> /tmp>

If you don't care what kind of shell you have, but need a login-shell,
then make the actual shell a login-shell by one of the 2 ways:

1) exec - bash
2) exec bash -login

--
Franz Zahaurek f...@gams.at
Vienna, Austria, Europe http://www.fzk.at

0 new messages