Why does my .bashrc get read when I run noninteractive commands over ssh

PSkocik

I've prepended each of my bash config files (~/.bashrc,~/.bash_profile,~/.profile) with echo NAME_OF_FILE, i.e. I get '.bashrc' when I source in ~/.bashrc.

What baffles me is why I get and indication that ~/.bashrc gets included when I run a command over ssh. E.g., if I do:

ssh localhost echo hi

I get

.bashrc
hi

Why is getting ~/.bashrc sourced in in this context? Shouldn't it NOT get sourced in since this should run an non-interactive bash session?

Indeed, ssh localhost tty gets me a 'not a tty' (preceded by '.bashrc' indicating that ~/.bashrc gets sourced in nonetheless).

I've grepped all my config files for commands sourcing in ~/.bashrc explicitly, and there are none that explain it.

(I only have tty -s && shopt -q login_shell && [[ -r ~/.bashrc ]] && . ~/.bashrc in my .bash_profile so that I get '.bashrc' even in interactive login shells, but this doesn't explain the ssh issue—I can comment it out and I still get the same behavior with the above ssh examples)

How can I debug this?

Lqueryvg

From bash man page:

Bash attempts to determine when it is being run with its standard input
connected to a a network  con‐nection,  as  if  by  the remote shell daemon,
usually rshd, or the secure shell daemon sshd.  If bash
determines it is being run in this fashion, it reads and executes commands
from  ~/.bashrc,  if  that file  exists and is readable.

I.e. ~/.bashrc will get run when you invoke it via ssh, regardless of whether you have a tty or not.

If you only want your .bashrc to run when you are interactive, try this at the top:

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

If that doesn't work, try this:

[ -z "$PS1" ] && return

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does my Image stretch when I run my app?

From Dev

why does my image move when i hover over it

From Dev

How do I get ssh to read the destination's `.bashrc`?

From Dev

Why does xrandr give me errors if I try and use commands on my computer, but not if I ssh into it?

From Dev

Why does my script not run the if statement when I type 5?

From Dev

Why am I seeing "xset: bad font path element" errors when I run a program over ssh?

From Dev

Why does SSH kill the colour in my /etc/update-motd.d/ script when force_color_prompt=yes is set in ~/.bashrc?

From Dev

I sourced my .bashrc file and now get: “-bash: /usr/bin/whoami: Argument list too long” so that I can't execute any commands. Using ssh

From Dev

Why does my activity crash when I try to get a number?

From Dev

Why does Android Studio not deploy my latest changes to my device when I run my project?

From Dev

When I start XTerm, my .bashrc doesn't get sourced

From Dev

Why does ssh look for keys in /root/.ssh when run with sudo?

From Dev

Automatically run commands over SSH on many servers

From Dev

Log Commands Run Over SSH on Client Side

From Dev

How to run subshell commands over SSH?

From Dev

How to run commands in batch mode over ssh?

From Dev

How do I restore my PATH in ~/.bashrc when basic commands don't work?

From Dev

Why does my javascript countdown timer occasionally over-run?

From Dev

Why does Twitter say my status update is over 140 characters when I only posted 130?

From Dev

Why does my hover button dropdown not stay open when I mouse-over?

From Dev

Why does Twitter say my status update is over 140 characters when I only posted 130?

From Dev

Why does rsync read .bashrc and not .profile?

From Dev

Why does my code print [I@87816d when i run this code?

From Dev

Why does the termination of a noninteractive bash process affect or not affect its child process, when running a script?

From Dev

Why must I run . ~/.bashrc each time I want to access my shortcuts there?

From Dev

Why does the "password" prompt take forever when I SSH into my Ubuntu 9.05 server?

From Dev

Debian 10 over SSH ignoring `DEBIAN_FRONTEND=noninteractive`

From Dev

Source by bashrc when I exit an 'ssh' session

From Dev

Why do I get different row orderings when I run my query with sp_executesql?

Related Related

  1. 1

    Why does my Image stretch when I run my app?

  2. 2

    why does my image move when i hover over it

  3. 3

    How do I get ssh to read the destination's `.bashrc`?

  4. 4

    Why does xrandr give me errors if I try and use commands on my computer, but not if I ssh into it?

  5. 5

    Why does my script not run the if statement when I type 5?

  6. 6

    Why am I seeing "xset: bad font path element" errors when I run a program over ssh?

  7. 7

    Why does SSH kill the colour in my /etc/update-motd.d/ script when force_color_prompt=yes is set in ~/.bashrc?

  8. 8

    I sourced my .bashrc file and now get: “-bash: /usr/bin/whoami: Argument list too long” so that I can't execute any commands. Using ssh

  9. 9

    Why does my activity crash when I try to get a number?

  10. 10

    Why does Android Studio not deploy my latest changes to my device when I run my project?

  11. 11

    When I start XTerm, my .bashrc doesn't get sourced

  12. 12

    Why does ssh look for keys in /root/.ssh when run with sudo?

  13. 13

    Automatically run commands over SSH on many servers

  14. 14

    Log Commands Run Over SSH on Client Side

  15. 15

    How to run subshell commands over SSH?

  16. 16

    How to run commands in batch mode over ssh?

  17. 17

    How do I restore my PATH in ~/.bashrc when basic commands don't work?

  18. 18

    Why does my javascript countdown timer occasionally over-run?

  19. 19

    Why does Twitter say my status update is over 140 characters when I only posted 130?

  20. 20

    Why does my hover button dropdown not stay open when I mouse-over?

  21. 21

    Why does Twitter say my status update is over 140 characters when I only posted 130?

  22. 22

    Why does rsync read .bashrc and not .profile?

  23. 23

    Why does my code print [I@87816d when i run this code?

  24. 24

    Why does the termination of a noninteractive bash process affect or not affect its child process, when running a script?

  25. 25

    Why must I run . ~/.bashrc each time I want to access my shortcuts there?

  26. 26

    Why does the "password" prompt take forever when I SSH into my Ubuntu 9.05 server?

  27. 27

    Debian 10 over SSH ignoring `DEBIAN_FRONTEND=noninteractive`

  28. 28

    Source by bashrc when I exit an 'ssh' session

  29. 29

    Why do I get different row orderings when I run my query with sp_executesql?

HotTag

Archive