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.

On my Internet research, I found the following command as solution to find the process name of a window:

xprop _NET_WM_PID | sed 's/_NET_WM_PID(CARDINAL) = //' | ps `cat`

I just do not understand what happens here after the first |.

Let's assume that xprop _NET_WM_PID outputs 1000 as process ID. What happens next? Why do we have a cat after ps? I am a bit confused.

share|improve this question

1 Answer 1

up vote 15 down vote accepted

xprop ... | sed ... is executed first, then cat reads its output. Due to the use of backticks, the output of cat is substituted in ps `cat` such that the command becomes ps 1000.

An alternative command that leads to the same result is:

ps $(xprop _NET_WM_PID | cut -d= -f2)

This works as follows:

  1. Execute xprop _NET_WM_PID to retrieve the process ID of a window. After clicking a window, it outputs something like:

    _NET_WM_PID(CARDINAL) = 12345
    
  2. Split at the = and take the second field. cut -d= -f2 takes the _NET_WM_PID(CARDINAL) = 12345 string from standard input and writes 12345 to standard output.
  3. Finally run the ps command with $(...) substituted for the output of ..., the command that gets executed is ps 12345. (side note: `...` can also be used instead of $(...), though there are some differences)
share|improve this answer
3  
Interesting "complexion", this | command 'cat' (I can't embed backticks in the backtick in comments?) ... the usual way is : | xargs command (with additionnal parameters to xargs, for example to limit to 1 or n arguments, etc) –  Olivier Dulac Apr 18 '13 at 13:58

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.