Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to have a function in .bash_login that displays text if the current terminal is XTerm, but NOT Terminology. When I do echo $TERM in Terminology, it outputs xterm How can I tell them apart?

share|improve this question

2 Answers 2

I don't know if this is the 'right' way to do it, but you could find what command the parent terminal was invoked with by calling ps with parent process ID obtained via the shell builtin $PPID e.g.

# get the shell's parent command from the PPID via ps
pcomm=$(ps -ocomm= $PPID)

You can then test the value of the command string - for example using a case statement

# now do something based on the value of the parent terminal command
case "$pcomm" in
  "gnome-terminal")
  echo "parent is gnome-terminal"
  ;;
  "xterm")
  echo "parent is xterm"
  ;;
  "terminator")
  echo "parent is terminator"
  ;;
  *)
  echo "unknown parent terminal"
  ;;
esac
share|improve this answer

Here is a another (simpler IMO) way of doing it. I don't have enlightenment, so I can't test it on Terminology, but I did test it with gnome-terminal, terminator and xterm.

terminalPID=$(ps -o ppid= $PPID) # get the script's parent's PID (the terminal)
processName=$(ps -p $terminalPID -o comm=) # get the terminal's name by it's PID
terminalName='gnome-terminal' # specify what process name you want to check for

# do the checking

if [ $processName == $terminalName ] ; then 
    echo "You are using gnome-terminal!"
else 
    echo "You are using something else!"
fi

Now in order to figure out your terminal's process name, open your terminal and run

ps -p $PPID -o comm=

This will print out the terminal's process name (providing you are running bash of course).

A fuller version that checks for several different terminal emulator's I have installed:

terminalPID=$(ps -o ppid= $PPID) # get the script's parent's PID (the terminal)
processName=$(ps -p $terminalPID -o comm=) # get the terminal's name by it's PID

# do the checking

if [ $processName == 'gnome-terminal' ] ; then 
    echo "You are using gnome-terminal!"
elif [ $processName == 'xterm' ] ; then
    echo "You are using xterm!"
elif [ $processName == '/usr/bin/termin' ] ; then
    echo "You are using Terminator!"
else 
    echo "You are using something else!"
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.