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 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

2 Answers 2

up vote 5 down vote accepted

This function should do the job:

container() {
    pid=$$
    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 ;;
        esac
        [[ $(echo $pid) == 1 ]] && break
    done
}
container
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

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.