This interesting case deserves a better answer (which I'm borrowing from this answer on superuser).
My original answer below depends on the fact that
ssh user@server command is supposed to execute command in a non-interactive shell, thus keeping the fault
.bashrc from being executed.
The problem lies with the remote shell. If it is Bash, then there is a special features that makes it execute those rc files anyway:
(man bash) Bash attempts to determine when it is being run with its standard
input connected to a network connection, as when executed 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. It will not do this if invoked as sh. The
--norc option may be used to inhibit this behavior, and the
--rcfile option may be used to force another file to be read, but
sshd generally invoke the shell with those
options or allow them to be specified.
That means that if your remote shell is Bash, then even if you run
ssh user@server 'rm .bashrc', your
.bashrc will be still executed first and your never-ending loop will prevent the
rm command from being executed.
scp won't save you since it is also a command executed internally by
sshd by calling
sh -c scp ....
The only exception is SFTP.
sshd has an SFTP server built-in. If
sshd is configured to use this internal SFTP server instead of an external one, then the server won't be started with
bash -c ....
So, if your remote default shell is Bash, your only option is to use the "Download-edit-upload" method below, not with
scp but with
Depending on your installation, here are several options:
ssh -t user@server 'vi ~/.bashrc'
bash without your
ssh -t user@server '/bin/bash --noprofile --norc'
Run another shell:
ssh -t user@server /bin/sh
sh is an example; you may try any other shell that is available on your server, like
Download-edit-upload you faulty
$ scp user@server:~/.bashrc /tmp/bashrc
$ vi /tmp/bashrc
$ scp /tmp/bashrc user@server:~/.bashrc