As an example I am using xdotool to obtain the list of windows on my desktop, which outputs a list of numbers, one on each line.

An example command for that is: xdotool search --name "a_program", which lists all the windows, which contain the string a_program in their title.

An example output would be:


An example command for getting the window title from the number is xdotool getwindowname 50854718.

How would I construct a command using xargs so that the output on every line would be translated to xdotool getwindowname outputnumber?

The question is mainly about xargs, though any comments about xdotool would be welcome.

2 Answers 2

> echo "50853718 
50859414" | xargs -n 1 echo xdotool getwindowname
xdotool getwindowname 50853718
xdotool getwindowname 50853807
xdotool getwindowname 50859414

If that's what you need then remove the echo.

xdotool search --name "a_program" | xargs -n 1 echo xdotool getwindowname
  • I made an error in the question which I've corrected. The output is each on a separate lines My intention is to have the xargs xdotool command on the same line as the first xdotool command in the way xargs command are usually constructed. Would something on the lines of xdotool search --name "a_program" | xargs -n 1 echo xdotool getwindowname be correct?
    – vfclists
    Jun 29, 2014 at 18:59
  • 1
    @vfclists By default xargs uses both space and newline as separator. But I have modified my example to make that clear. Your example looks good. Using echo you can just give it a try. Jun 29, 2014 at 19:20
  • It would be better if you include the content of you comment to the answer.
    – Braiam
    Jun 29, 2014 at 19:25
  • If there was more than one item per output line eg val1 val2 val3 would it be possible to pass them to xargs using something like this like this after the pipe symbol xargs -n 3 xdotool --opt1=$1 --opt2=$2 --opt3=$3 where $1, $2 etc would be the output line split by the spaces?
    – vfclists
    Jun 29, 2014 at 22:12
  • 1
    @vfclists That is not possible. You need a wrapper script for that: xargs -n 3 /wrapper/script.sh The script is a one-liner (after the shebang line): xdotool --opt1="$1" --opt2="$2" --opt3="$3" Jun 29, 2014 at 22:21

For completeness: you can do it without xargs as well, if you are using Bash or a compatible shell. (I'm not sure which other shells accept this syntax but I would imagine several of the common ones do.) The benefit is only that you don't invoke a separate program (namely xargs), but that's unlikely to be noticeable in practice.

for winid in $(xdotool search --name "a_program"); do
    xdotool windowname $winid;

Spacing doesn't matter so you can put that all on one line. However this does rely on the fact that the individual window IDs returned by xdotool search do not have any whitespace in them.

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.