In case a simple return
doesn't do the job, here's another approach taken from this blog article:
if `tty -s`; then
mesg n
fi
tty -s
checks if there's a TTY attached (the -s
tells it to do so silently and just exit with the appropriate return code). tty
returns the tty attached (e.g. "/dev/pts/1"). This should be safer than checking some shell variable ;)
mesg
controls the write access to your terminal (msg n
disallows writing to the (in our case non-existing) terminal), and thus requires one to be present.
On some systems (in my case Debian Jessie, but there are also reports on Ubuntu) mesg n
1 is set unconditionally in either ~/.bashrc
or ~/.profile
. So if it exists that way, this might be the culprit.
As with the other examples, you can of course make that a one-liner: [[ $(tty -s ) ]] && mesg n
. And nobody keeps you from combining the two:
if [[ $(tty -s ) ]]; then
mesg n
else
return
fi
Btw: According to the linked article, this fragment should go to the .bashrc
of the machine you connect to (the "remote") – so if that's johndoe@somehost
, this should be applied at the start of /home/johndoe/.bashrc
on somehost
. In my case I only got rid of the message after having applied this change on the "calling host" as well.
PS: Also check the .profile
if it has a stand-alone msg n
command (it did in my case). If it does, wrap it there.
1: mesg n
is used to prevent other users on the machine writing to your current terminal device, which per se is a good thing – but not helpful for some rsync
job ;)