7

How can I get the window ID of the terminal that my shell script is running in?

Background: I need to write a script to do something based on which virtual desktop it is running in. To get the virtual desktop (not current desktop, I may change it while the script is running), I need the window ID of the terminal that the script is running in. If the script is not run from a terminal, it should fail with an error.

3
  • 1
    I know very little of X11, but running env in my terminal, I spotted an environment variable WINDOWID, that shows the same number in different tabs in the same terminal, but a different number in another terminal. Perhaps that's what you need? Commented Mar 7, 2014 at 12:47
  • Also: stackoverflow.com/questions/20580897/… Commented Mar 7, 2014 at 12:48
  • @Evert that is an answer (your first comment about the environment variable), can you move it to be an answer. Then I will give you a big tick. Commented Mar 7, 2014 at 13:07

1 Answer 1

10

I know very little of X11, but running the env command in my terminal, I spotted an environment variable WINDOWID, that shows the same number in different tabs of the same terminal, but a different number in another terminal. Perhaps that's what you need?

It does appear to be an Xterm thing (and probably any terminal that mimicks Xterms behaviour. The xterm(1) man page only says:

ENVIRONMENT

 Xterm sets several environment variables: ... WINDOWID is set to the X window id number of the xterm window. 

Some other possibly relevant questions with answers:

Sign up to request clarification or add additional context in comments.

1 Comment

While working in xterm and rxvt, it is not available in terminator.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.