Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the type of terminal (classic Unix terminal vs graphical terminal)

Tags:

bash

I'm configuring my prompt (PS1) via .bashrc and found one issue with my current configuration: I am using a 256 color scheme. This is not compatible with the classical terminal (accessible via e.g. Ctrl+Alt+F2) but looks beautiful in graphical terminals such as gnome-terminal, terminator, etc.

So I have to change my prompt depending on the type of terminal. To do this, I need a condition for if clause to test the type of terminal. Do you know how to do this?

like image 522
prcastro Avatar asked Dec 11 '22 13:12

prcastro


2 Answers

the TERM variable indicates the terminal type. when running in an x-terminal, it is usually xterm (but can also be xterm-color-256 as Dmitry has hinted in his answer). the following code checks whether the value of $TERM starts with xterm (and thus catches several cases):

case "$TERM" in
   xterm*)
      echo "running as an x-terminal"
      ;;
   *)
      echo "not running as an x-terminal"
      ;;
esac
like image 52
umläute Avatar answered Apr 29 '23 14:04

umläute


echo $TERM would give you the terminal type

like image 42
Varun Avatar answered Apr 29 '23 14:04

Varun