bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: .bashrc is sourced even for non-interactive shells (when run from ss


From: Pierre Gaston
Subject: Re: .bashrc is sourced even for non-interactive shells (when run from sshd)
Date: Sat, 2 Jun 2012 20:19:33 +0300

On Sat, Jun 2, 2012 at 8:15 PM, Mikel Ward <address@hidden> wrote:
> bash sources .bashrc even for some non-interactive shells.
>
> For example with
>
>    echo \$- is $-
>
> in ~/.bashrc, and shell set to /bin/bash (bash 4.2.28)
>
>    ssh -n -T localhost true
>
> produces the output
>
>    $- is hBc
>
> I assume this is caused by this code in shell.c
>
>     if (run_by_ssh || isnetconn (fileno (stdin)))
>
> The man page says
>
>       When an interactive shell that is not a login shell is started,
> bash reads and executes commands from ~/.bashrc,
>
> but makes no mention of the special handling for ssh and rsh.
>
> This seems to have been the case since at least bash 2.02.
>
> I'd argue this is a misfeature, but I guess that ship has sailed.  Can
> the man page at least be updated?
>
> Thanks
>
>From http://mywiki.wooledge.org/DotFiles:

"Remote non login non interactive shells"
Bash has a special compile time option that will cause it to source
the .bashrc file on non-login, non-interactive ssh sessions. This
feature is only enabled by certain OS vendors (mostly Linux
distributions). It is not enabled in a default upstream Bash build,
and (empirically) not on OpenBSD either.

If this feature is enabled on your system, Bash detects that
SSH_CLIENT or SSH_CLIENT2 is in the environment and in this case
source .bashrc. eg suppose you have var=foo in your remote .bashrc and
you do: ssh remotehost echo \$var it will print foo.

This shell is non-interactive so you can test $- or $PS1, if you don't
want things to be executed this way in your .bashrc.

Without this option bash will test if stdin is connected to a socket
and will also source .bashrc in this case BUT this test fails if you
use a recent openssh server (>5.0) which means that you will probably
only see this on older systems.

Note that a test on SHLVL is also done, so if you do: ssh remotehost
bash -c echo then the first bash will source .bashrc but not the
second one (the explicit bash on the command line that runs echo).

The behaviour of the bash patched to source a system level bashrc by
some vendors is left as an exercise.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]