just put following into top of ~/.bashrc on username of id on remote machine if that id uses bash
# If not running interactively, don't do anything and return early
[[ $- == *i* ]] || return
which simply exits early from the ~/.bashrc instead of sourcing entire file ... this solves making .bashrc silent when you are not logging into that id and are just running your scp or sftp with that username as the remote id ... to quote @Peter Scott in other answer : "Put simply, .bashrc and .bash_profile etc have to be silent or they interfere with the sftp / scp connection protocol."
Alternatively, if that remote id uses zsh then put following at top of its ~/.zshrc
# If not running interactively, don't do anything and return early
[[ -o interactive ]] || exit 0
If the shell on your remote machine does not use ~/.bashrc then make above edit in file ~/.bashrc_profile or ~/.profile or similar to suit your shell on that remote box
.... UPDATE ... by putting above snippet into top of file ~/.bashrc for user on remote box any ssh connection will source file ~/.bashrc which will simply read ~/.bashrc until is sees this snippet then stop continuing to source the remainder of file ~/.bashrc so as to avoid polluting the shell on that remote box with things ssh connection does not need nor want ... do NOT put snippet into any file which must get sourced or executed to completion as the point of this snippet is to exit early from sourcing file ~/.bashrc ( NOTE there is a difference between sourcing a file versus executing a file ) ... file ~/.bashrc is always sourced never executed
/etc/profile.d
and in~/.bashrc
to diagnose some other problem (which was: why is there a delay on ssh-ing into a remote server? Turns out it was because a script to ssh had-Y
as an argument tossh
and was waiting for an X server on the client, which wasn't running) - anyway, rather than delete the echo statements I'd put in various login scripts, I wondered if echoing the messages to stderr instead of stdout would avoid the sftp error and it did, e.g. `>&2 echo "info: this is my_script.sh". Not enough rep!~/.bashrc
just comment those.