I am trying to find a way to determine the number of open tabs in a gnome-terminal window. Currently, the best option seems to be xdotool. I can move to the next tab using

xdotool key ctrl+Page_Down

the only problem is if there is only a single tab open. Then the above command outputs garbage into the terminal window. The garbage are these three characters ;5~ (which is probably the character representation for Ctrl+Page_Down).

How can I avoid xdotool or gnome-terminal from outputting these three characters? (Off topic: I would also like to know if anyone knows if there are any other ways to determine the number of open tabs in a gnome-terminal window)

2 Answers 2


From a shell in the terminal you want to count the tabs on something like this might work

ps -h --ppid `ps -ho ppid $$` | grep pts | wc -l

The tabs share a common parent process and will be associated with a pseudo-terminal (pts). The $$ variable contains the process id of the invoking shell. The -h switch tells 'ps' to suppress the default header line and the -o switch tells it to use a custom format to return just the parent process id. The backticks say return the value of this command. So the output of the backticks will be the numeric process id of the parent of the current shell. Passed to 'ps' with the --ppid switch returns all processes with that parent. Filter with grep for TTY association and count lines.

  • Thanks! Looks interesting.. I will check this out.. Could you add some explanation why it should work? Apr 9, 2015 at 19:40
  • Here is the long hand Apr 9, 2015 at 19:57
  • 1
    Thanks for the explanation! It seems there is a problem with this approach when I open more than one terminal window. It finds all tabs in all windows, instead of only the tabs in the current window.. but only if the command is run from the child terminal window. The parent terminal window still shows correct tab number.. Apr 9, 2015 at 20:24

method #1

Oneliner to be executed in the same window that you wann a count :

expr $( echo $( xprop -root | awk '/_NET_ACTIVE_WINDOW/ && /0x/ {print $5}' | xargs xwininfo -all -id | awk '/child/ || /children/ {print $1}' ) ) / 2

Here's the one-liner in action and reports correctly 5 open tabs enter image description here


What I've noticed about gnome-terminal is that when you run xwininfo -all -id 0xXXXXXXX (where 0xXXXXX is numeric id of that window), it reports 3 children for one single window, 5 children for a window with 2 tabs, 7 children for 3 tabs, and so on. See the pattern ? If you do integer division (i.e., only get the whole number when you divide children by 2, you get the number of tabs).

So that's what that line is basically doing expr $(stuff that gets number of children) / 2

How do I get the number of children ? I search the output of xprop -root for the string that has numeric id of active window, give that numeric id to xwindowinfo -all -id as argument, and then search that output for number of children.

Advantage of this method ? Gives exact number of tabs in the window you run it. Disadvantage ? only works in gnome-terminal, doesn't work in sakura

method #2 (crude and not as good)

There's another way that I've found but it only works for one window. If you run who -a you will see something like this:

serg  + pts/7        2015-04-11 08:47 00:02       17540 (:0)
serg  + pts/2        2015-04-11 08:27 00:12       17540 (:0)
serg  + pts/12       2015-04-11 08:50 00:08       17540 (:0)
serg  + pts/13       2015-04-11 08:56 00:01       17540 (:0)
serg  + pts/14       2015-04-11 08:58   .         17540 (:0)

Notice the + sign ? Yup, if you are in a single window, then you could run this command in that same window who -a | grep "+" | wc -l. Now, Kenneth Stevenson properly mentioned that they share same pid and are associated with pseudo terminal ? Yup, all windows do that. So if you have another window open, this method will give you incorrect result.

  • If you like my answer, please upvote. If my answer solves your question, please press the grey check mark to accept the answer. Thanks ! Apr 11, 2015 at 16:28
  • I cannot get this to work..If I open a gnome-terminal window with four tabs gnome-terminal --tab -t t1 --tab -t t2 --tab -t t3 --tab -t t4 and then type xwininfo -all -id $WINDOWID in the new terminal. It gives me 3 children, not 9.. Apr 12, 2015 at 18:03
  • @HåkonHægland When you manually open four tabs then; t2 is child of t1, t3 is child of t2 and t4 is child of t3. Nov 11, 2020 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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