Unix & Linux
bash ssh cygwin sshd
Updated Sun, 17 Jul 2022 12:43:50 GMT

Why doesn't my new cygwin .bashrc interactive check work?


I just updated Cygwin and the new .bashrc contains this line right at the top:

echo "BEFORE TEST"
# If not running interactively, don't do anything
[[ "$-" != *i* ]] && return
echo "AFTER TEST"

I understand if not running interactive, you don't need to do anything, but my problem is that if I ssh with a command:

ssh localhost pwd

I see this:

BEFORE TEST

So, obviously, it fails the [[ "$-" != *i* ]] test and returns. Obviously ssh localhost pwd is a trivial example, but I expect .bashrc to be run all the way through if running a command through ssh. My actual automation runs commands remotely against this cygwin instance and is failing because the path (which is set by my .bashrc) is not getting set correctly.

Furthermore, can anyone explain what that test is actually doing? What is i? What is $- supposed to represent?




Solution

If you run a single command (pwd in your case) through ssh, it is not an interactive shell, so the behavior is correct, in my opinion.

You should set your PATH in ~/.profile or ~/.bash_profile, not in ~/.bashrc.

As found in bash(1) man page:

   PARAMETERS
      (...)
      Special Parameters
          The shell treats several parameters specially.   These  parameters  may
          only be referenced; assignment to them is not allowed.
          (...)
          -      Expands  to  the  current option flags as specified upon invoca-
                 tion, by the set builtin command, or  those  set  by  the  shell
                 itself (such as the -i option).

So i contained in $- means that the -i option has been used (or automatically set by the shell, being interactive).