sajad torkamani

xargs is a Unix command-line utility that lets you convert input from standard input into an argument to a command.

Syntax

<command1> | xargs <command-2>

Here the output of <command1> will be passed as an argument to <command-2>

Example

whoami | xargs echo "I am logged in as:"

Assuming whoami outputs “sajad” to standard input, xargs will take “sajad” and pass it as an argument to echo "I am logged in as:" so that the command essentially becomes:

echo "I am logged in as: " <stdin>

# Or in this case
echo "I am logged in as: " sajad

Another example

Combining xargs with other commands like grep or awk lets you do things like:

npm outdated | grep fortawesome |  awk '{print $1}' | xargs npm update

This command will update all your NPM packages that contain fortawesome in the package name. That’s quite handy.

Tagged: Bash

Leave a comment

Your email address will not be published. Required fields are marked *