Shell – Detect if running in a virtual terminal

consoleshell-scriptterminaltty

I'd like to autologin to tty1 on login and then use vlock to lock it.

How can I detect from Bash if the current terminal is a console virtual terminal (e.g. tty1), so that I can put the vlock line into .bash_profile and have it run only if logging in through tty1?

Best Answer

You can use tty to get the name of the current virtual terminal, then test against it with a case statement:

#!/bin/sh

case $(tty) in /dev/tty[0-9]*)
    vlock ;;
esac