Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to check for SSH session in shell

📅 2016-Mar-07 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

There are times when you want to check if you are in a SSH session from your shell. For example, I like to change the shell prompt based on whether it is in a local machine or in a SSH session on another machine.

An easy way to do this is to check if any environment variable related to SSH has been set. For example, I check if the SSH_CLIENT variable is set in my shell. In Fish, this can be done by checking the result of set --query SSH_CLIENT for example.