How to distinguish between Rsync and SSH access in a “.bashrc” script

Solution 1:

It actually sounds like you actually want to distinguish between interactive and batch SSH access.

Rsync still goes over SSH – as does SFTP, and Git, and Borg, and several other tools, and even ssh example.host some_command. What they have in common is that they're non-interactive, and do not allocate a tty – all of them want SSH to provide a clean data tunnel, and all equally have no use whatsoever for $PS1 or bash_aliases. There's nothing really special about Rsync here.

To distinguish these two modes, add a $- check near the top of your ~/.bashrc:

[[ $- == *i* ]] || return 0

Most importantly, this should go before anything that would produce output. Usually it can just be done as the first thing in your ~/.bashrc (except perhaps after setting environment variables like PATH).