I am trying to write out a dynamic bash profile for a few machines, and was wondering if there is a variable that allows .bashrc
if it is being accessed remotely. I've seen a few examples using X variables, but that is irrelevant to both machines.
if [ "$SSH_CONNECTION" ]; then
echo I am remote
else
echo I am local
fi
When you connect via ssh, your bash process is a child of sshd ($PPID is a variable of bash's parent process - ssh that is, if you connect remotely). You can check for that:
if ps ax | grep ^$PPID'.*sshd' &> /dev/null; then
# do your stuff
fi
Edit: I was bored and used time
to get execution times and found out that this version apparently is a couple of milliseconds faster:
if grep ^sshd: /proc/$PPID/cmdline &> /dev/null; then
# do your stuff
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With