xargs -o
I recently found out about the -o
option for xargs
!
From man xargs
:
-o, --open-tty
Reopen stdin as /dev/tty in the child process before executing the command. This is useful if you want xargs to run an interactive application.
If you don't do this, any commands that require input (even a confirmation prompt!) will fail when run with xargs.
Specific use-case: I have a file called packages
, and I want to remove all of these packages. I can't run cat packages | xargs pacman -R
because the confirmation step will fail.
However, by using cat packages | xargs -o pacman -R
, I can now complete the confirmation prompt normally! (I could also use --noconfirm
, but this seems cleaner.)
Under the hood, it seems that xargs
will read from stdin until it sees EOF, and use this as the command line args. It will then reopen the stdin stream of the current tty (by directly using /dev/tty
) and use that as fd 0 for the child process it launches.
Neat stuff! Honestly kinda wish this was enabled by default. Though I can imagine it might misbehave inside scripts, so maybe not.
If you post a reply on another blog or social media, or just want to chat, email me! christopher@cg505.com