Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have gnome-terminal and Guake installed. I want to give different startup messages to be displayed on these terminal emulators as they start. What code should I write in .bashrc so that this is achieved?

share|improve this question
up vote 8 down vote accepted

This function should do the job:

container() {
    while true; do
        pid=$(ps -h -o ppid -p $pid 2>/dev/null)
        case $(ps -h -o comm -p $pid 2>/dev/null) in
        (gnome-terminal) echo "Running in gnome terminal";return;;
        (xterm) echo "Running in xterm";return;;
        (rxvt) echo "Running in rxvt";return;;
        (python) if [ ! -z "$(ps -h -o args -p $pid 2>/dev/null | grep guake)" ]; then echo "Running in Guake"; return; fi ;;
        [[ $(echo $pid) == 1 ]] && break
share|improve this answer
Nice answer! Don't forget to actually call the function with container after the definition. – rosch Oct 31 '12 at 18:43
I expected this to be obvious but you are right, answer updated accordingly. – jlliagre Oct 31 '12 at 21:51
@jlliagre I tried the code. It works smoothly on gnome-terminal but it has a problem on Guake. It doesn't give any output. Instead it kind of freezes as if in deadlock. I have to Ctrl+C it to use it. Didn't really understand the code so don't know where it is going wrong. – VedVals Nov 1 '12 at 11:59
Code fixed. I missed guake is run by python and had a bug preventing the function to exit. Thanks for the feed-back. – jlliagre Nov 1 '12 at 16:40
Sorry dude, still not quite working. Gives error bash: [: too many arguments. Got bash v4.2.24, python v2.7.3 if it helps. – VedVals Nov 3 '12 at 11:29

You can get the terminal emulator name, by grepping the parents process name. Therefore it works with every terminal emulator.

In bash, zsh, etc.:

basename "/"$(ps -f -p $(cat /proc/$(echo $$)/stat | cut -d \  -f 4) | tail -1 | sed 's/^.* //')

With fish-shell:

basename "/"(ps -f -p (cat /proc/(echo %self)/stat | cut -d \  -f 4) | tail -1 | sed 's/^.* //')
share|improve this answer

Try this:

echo $TERM

This is more authoritative, but could be messed up by your programs. However on mine, it says xterm and on ttys it says linux, which I think stands for Linux Console.

share|improve this answer

Your Answer


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.