I use a coloured bash prompt like so:
blue=$(tput setaf 4)
reset=$(tput sgr0)
bold=$(tput bold)
export PS1='($?) \[$bold$blue\]\u@\h:\w\[$reset\]$(__git_ps1)> '
I usually work in a terminal window (gnome-terminal) but sometimes in a full-screen console (CTRL+ALT+F3).
My coloured prompt is fine in the GUI terminal but hard to read in full-screen because the colours look different. So I'd like to use a different colour (e.g. cyan=$(tput setaf 6)
), when in console window. For this to work I need to figure out which mode I'm currently in and set my $PS1
accordingly.
I tried the tty
command. It gives
/dev/pts/0 # GUI terminal
/dev/tty3 # console
I also tried to examine the value of $TERM
. It gives
xterm-256color # GUI terminal
linux # console
Both of them would work for me but my question is: Is that the correct way to reliably distinguish console vs. terminal or are there better ways? I'm on 17.10.1 if that matters.
There are a number of ways to determine that, three famous being:
tty
- print the file name of the terminal connected to standard input:This function written by Sergiy Kolodyazhnyy makes use of
tty
.ps hotty $$
(short forps --no-header --format tty --pid $$
):who who whom | awk '{ print $2 }'
(in fact that'swho
with two arbitrary arguments, equal towho -m
– which also matches the pun):I suspect the values of
TERM
to differ between distributions and even releases, buttty
is a stable and reliable way. I would use it like so:There seems to be a problem with at least
who
ingnome-terminal
, luckily there's a wrapper script to work around this issue.Helpful links: