I'm using tcsh, and have to source a group .cshrc file. This file echoes some messages, which is fine for normal shells, but causes problems with programs like scp and rsync. I can see the solution taking one of a few forms, but am unable to implement any of them.

Only execute echos when appropriate

I've scoured the rsync and tcsh man pages, but I can't find any variables that are guaranteed to be set or unset when it is called from ssh/rsync/whatever. $PROMPT is the same as normal, $shlvl is 1, and nothing else looks promising.

Redirect to stderr

rsync/scp/etc don't seem to care about what comes over stderr, so if I could, I would

echo $MSG >&2

But this doesn't even work from the shell. Instead, it writes $MSG to a file named 2. When I look through the history, it seems that something (xterm? readline? tcsh?) is inserting spaces, so what was actually run was

echo $MSG > & 2

So the observed behavior makes sense given the actual input to tcsh.

Redirect to /dev/stderr

I've also tried

echo $MSG > /dev/stderr

Which works for ssh, but for scp and rsync, I get the message '/dev/stderr: Permission denied.' and the key difference seems to be where the file is symlinked. Adding ls -l /dev/stderr /proc/self/fd/2 to the cshrc file shows

However, since the permission denied message comes across on stderr, the scp/rsync process is able to do its thing, so I can live with this solution, but would rather not get this spurious error message.