LinuxCommandLibrary

fgconsole

Print or set the active virtual console

TLDR

Print current terminal number or serial if the terminal is serial

$ fgconsole
copy

Print the next unallocated virtual terminal
$ fgconsole [[-n|--next-available]]
copy

SYNOPSIS

fgconsole

DESCRIPTION

The fgconsole command outputs the number of the virtual console (VT) that is currently active and visible on the system's display. Linux systems typically provide several virtual consoles, accessible via Ctrl+Alt+Fn keys (e.g., Ctrl+Alt+F1 for console 1, Ctrl+Alt+F2 for console 2, etc.). This command is useful in scripts or for diagnostic purposes to determine which console is in the foreground, especially when trying to interact with a specific console or to understand the current terminal context. It directly queries the kernel's virtual terminal layer, providing a simple integer representing the active console ID.

CAVEATS

It only reports the number of the console (e.g., 1, 2, 3), not its device path (e.g., /dev/tty1).
It operates specifically on Linux virtual consoles and does not provide information about pseudo-terminals (PTYs) used by graphical terminal emulators (like gnome-terminal, konsole, xterm) or SSH sessions.

USAGE IN SCRIPTS

fgconsole is often used in shell scripts that need to perform actions dependent on the currently active console, such as logging information to a specific console or preventing certain operations if the user is not on a designated console. For example, if [ "$(fgconsole)" -eq 1 ]; then echo "You are on console 1"; fi.

DISTINCTION FROM PTYS

It's crucial to distinguish between virtual consoles (accessible directly from the physical keyboard/display) and pseudo-terminals (PTYs) used by graphical terminal emulators or SSH connections. fgconsole only provides information about the former, not graphical terminal sessions or remote connections.

HISTORY

The fgconsole command is part of the `kbd` package (Linux console tools), which provides essential utilities for managing the Linux console. It has been a standard utility for Linux systems for a long time, dating back to early kernel versions that supported virtual terminals, reflecting the fundamental need to programmatically determine the active console. Its simplicity underscores its core utility and enduring relevance.

SEE ALSO

chvt(1), openvt(1), deallocvt(1), console(4), vc(4), agetty(8)

Copied to clipboard