1

i want to get the result of the following command into a variable.

xprop -name "google-chrome-stable" | grep "window id" | awk '{print $5}' | awk '{print $1}'

the result should look something like that

OUTPUT=xprop -name "google-chrome-stable" | grep "window id" | awk '{print $5}' | awk '{print $1}'
echo $OUTPUT

I know it could be done with command substitution. The examples in the Link command substitution are not detailed enough because they dont explain how to escape the quotation marks and apostrophes. Could someone help me find out how to solve that?

divramod
  • 1,454
  • 2
  • 20
  • 35
  • What window id are you trying to get from that output? There are at least three lines that match "window id" on some of my windows here. – Etan Reisner Dec 31 '14 at 16:12
  • i am trying to get the window id for beeing able to use it with xdotool to place my windows on the screen. do you have any idea what these different "window ids" could be for? are there really different window ids in some of your windows? – divramod Jan 01 '15 at 15:19
  • If you run that `xprop` command by itself what line in the output exactly (and for what property) is the line you are looking for? And yes, a number of my windows have more than one line that matches a search for `window id`. – Etan Reisner Jan 01 '15 at 22:21

2 Answers2

1

It looks like you simply need to do this:

output=$(xprop -name "google-chrome-stable" | awk '/window id/{print $5}')
echo "$output"

There's no need to do any escaping within the $( ). I've wrapped the variable to be echoed in double quotes to prevent things like glob expansion from occurring. As a bonus, I've combined your grep and awk commands and removed the last one which wasn't doing anything useful.

I have also made your variable name lower case. Upper case variable names should be reserved for shell internals. You'll thank me for this one day when you have a variable called $PATH and suddenly everything stops working...

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
1

If you are trying to get the group leader window you can use the following instead.

output=$(xprop -notype -f WM_HINTS "32mx" ' ?m6($8)\n' -name google-chrome-stable WM_HINTS | awk '{print $2}')
echo "$output"

Arguments to -f were pulled from the xprop source itself and some man page reading about format and dformat.

The Description section of the xprop man page explains all about format and dformat.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • @TomFenech No. Thanks. That was a typo/context mistake (I'd been working with make a few minutes before that). – Etan Reisner Dec 31 '14 at 18:47
  • hey etan, i tried your xprop command with an open google-chrome but cant get a result from it. could you please explain in short what [1] -notype -f WM_HINTS "32mx" means, [2] what ' ?m6($8)\n' means, [3] -name 'DO NOT KILL' WM_HINTS means. I am actually searching for the google-chrome-stable leader to move this window with xdotool besides my terminal and give it a predefined size. [4] could you please explain what group leader window means for chrome? – divramod Jan 01 '15 at 15:27
  • @ArvidPetermann Bah, "DO NOT KILL" was a test window I was using. You need your window name there instead. Sorry about that. `-notype` tells `xprop` not to include type information in the output (just cleans up the output a little). `-f hint format dformat` specifies the format for hint. I could go into more detail but I'd be cribbing heavily from the man page and you can do that yourself if you care. Suffice it to say it asks for just the `WM_HINTS` group leader window id without the rest of the information. `WM_HINTS` on the end is asking for just that one field of information. – Etan Reisner Jan 01 '15 at 22:20