444
cat a.txt | xargs -I % echo %

In the example above, xargs takes echo % as the command argument. But in some cases, I need multiple commands to process the argument instead of one. For example:

cat a.txt | xargs -I % {command1; command2; ... }

But xargs doesn't accept this form. One solution I know is that I can define a function to wrap the commands, but I want to avoid that because it is complex. Is there a better solution?

3
  • 3
    Most of these answers are security vulnerabilities. See here for a potentially good answer. May 13, 2019 at 9:22
  • 3
    I use xargs for almost everything, but I hate putting commands inside strings and explicitly creating subshells. I'm on the verge of learning how to pipe into a while loop that can contain multiple commands. Jul 23, 2019 at 2:17
  • Test the solutions on inputs like: ", *, a two spaces b, $(echo Do not print this). If these do not work as expected, there are likely other bugs in the solution, too.
    – Ole Tange
    Feb 13, 2021 at 7:55

11 Answers 11

571
cat a.txt | xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$arg"; ...; done' _

...or, without a Useless Use Of cat:

<a.txt xargs -d $'\n' sh -c 'for arg do command1 "$arg"; command2 "$arg"; ...; done' _

To explain some of the finer points:

  • The use of "$arg" instead of % (and the absence of -I in the xargs command line) is for security reasons: Passing data on sh's command-line argument list instead of substituting it into code prevents content that data might contain (such as $(rm -rf ~), to take a particularly malicious example) from being executed as code.

  • Similarly, the use of -d $'\n' is a GNU extension which causes xargs to treat each line of the input file as a separate data item. Either this or -0 (which expects NULs instead of newlines) is necessary to prevent xargs from trying to apply shell-like (but not quite shell-compatible) parsing to the stream it reads. (If you don't have GNU xargs, you can use tr '\n' '\0' <a.txt | xargs -0 ... to get line-oriented reading without -d).

  • The _ is a placeholder for $0, such that other data values added by xargs become $1 and onward, which happens to be the default set of values a for loop iterates over.

25
  • 66
    For those unfamiliar with sh -c -- note that the semicolon after each command is not optional, even if it is the last command in the list. Sep 19, 2012 at 16:33
  • 7
    At least on my configuration, there must be a space immediately after the initial "{". No space is required before the ending curly brace, but as Mr. Sussman noted, you do need a closing semicolon.
    – willdye
    Oct 11, 2012 at 20:27
  • 5
    This answer previously had curly braces around command1 and command2; I later realized they're not necessary. Aug 23, 2013 at 22:40
  • 30
    To clarify the above comments about semicolons, a semicolon is required before a closing }: sh -c '{ command1; command2; }' -- but it's not required at the end of a command sequence that doesn't use braces: sh -c 'command1; command2'` Oct 30, 2013 at 17:49
  • 11
    If you're including the % character somewhere in your string passed to sh -c, then this is prone to security vulnerabilities: A filename containing $(rm -rf ~)'$(rm -rf ~)' (and that's a perfectly legal substring to have within a filename on common UNIX filesystems!) will cause someone a very bad day. Jun 23, 2017 at 22:34
78

You can use

cat file.txt | xargs -i  sh -c 'command {} | command2 {} && command3 {}'

{} = variable for each line on the text file

9
  • 20
    This is insecure. What if your file.txt contains a datum with $(rm -rf ~) as a substring? Jun 23, 2017 at 22:36
  • 10
    This worked well for me, luckily none of the zoneinfo timezone definitions contain rm -rf ;)
    – Kyle K
    Feb 27, 2021 at 14:35
  • 9
    +1. It's incredible how much effort people will spend on security where it is not needed (e.g. processing a list of IP addresses, PIDs or USB device names) Sep 22, 2021 at 20:11
  • 8
    And yet, as a "generic solution", the security concern should be (and was) rightly noted. The command should not be used on untrusted or unsanitized input unless you understand the risks. If you do trust your input, then have at it. Oct 13, 2021 at 16:12
  • hm I get permission denied for commands running inside sh -c (or bash -c)
    – Kasapo
    May 18, 2022 at 20:11
42

With GNU Parallel you can do:

cat a.txt | parallel 'command1 {}; command2 {}; ...; '

For security reasons it is recommended you use your package manager to install. But if you cannot do that then you can use this 10 seconds installation.

The 10 seconds installation will try to do a full installation; if that fails, a personal installation; if that fails, a minimal installation.

$ (wget -O - pi.dk/3 || lynx -source pi.dk/3 || curl pi.dk/3/ || \
   fetch -o - http://pi.dk/3 ) > install.sh
$ sha1sum install.sh | grep 883c667e01eed62f975ad28b6d50e22a
12345678 883c667e 01eed62f 975ad28b 6d50e22a
$ md5sum install.sh | grep cc21b4c943fd03e93ae1ae49e28573c0
cc21b4c9 43fd03e9 3ae1ae49 e28573c0
$ sha512sum install.sh | grep da012ec113b49a54e705f86d51e784ebced224fdf
79945d9d 250b42a4 2067bb00 99da012e c113b49a 54e705f8 6d51e784 ebced224
fdff3f52 ca588d64 e75f6033 61bd543f d631f592 2f87ceb2 ab034149 6df84a35
$ bash install.sh
8
  • 81
    Installing tools via running random scripts from unknown sites is horrible practice. Parallel has oficiall packages for popular distros, which can be trusted (to some extend) way more than random wget|sh...
    – mdrozdziel
    Dec 13, 2013 at 20:43
  • 7
    Let us see what is the easiest attack vector: Pi.dk is controlled by the author of GNU Parallel, so to attack that you would have to break into the server or take over DNS. To take over the official package of a distribution, you can often just volunteer to maintain the package. So while you might be right in general, it seems in this particular case your comment is not justified.
    – Ole Tange
    Dec 13, 2013 at 22:41
  • 15
    In practice I do not know that pi.dk belongs to the author. Actually verifying that this is the case, thinking of how to use ssl in wget and checking that this command does what it is supposed to do is a bit of work. Your point that the official package can contain malicious code is true, but that also holds for the wget package.
    – Fabian
    May 25, 2014 at 17:34
  • 6
    This might not be the best solution if each of the commands OP wants to execute must be sequential, correct?
    – Keenan
    Dec 13, 2015 at 7:45
  • 6
    @IcarianComplex Adding -j1 will fix that.
    – Ole Tange
    Dec 13, 2015 at 11:53
41

I prefer style which allows dry run mode (without | sh) :

cat a.txt | xargs -I % echo "command1; command2; ... " | sh

Works with pipes too:

cat a.txt | xargs -I % echo "echo % | cat " | sh
2
  • 2
    This works, until you want to use GNU xargs' -P option... (If not, I mostly use -exec on find, since my inputs are mostly filenames) Jul 12, 2018 at 11:45
  • Fails on input: "
    – Ole Tange
    Feb 13, 2021 at 7:49
29

This is just another approach without xargs nor cat:

while read stuff; do
  command1 "$stuff"
  command2 "$stuff"
  ...
done < a.txt
3
  • 5
    Buggy, as given. Unless you clear IFS, it'll ignore leading and trailing whitespace in the filenames; unless you add -r, filenames with literal backslashes will have those characters ignored. Jun 23, 2017 at 22:35
  • 2
    Does not answer the question. It specifically asked about xargs. (This is hard to expand to do something similar to GNU xargs' -P<n> option) Jul 12, 2018 at 11:40
  • 3
    This works perfectly well. You can also use it as a piped command like $ command | while read line; do c1 $line; c2 $line; done
    – Alexar
    May 3, 2020 at 12:14
24

This seems to be the safest version.

tr '[\n]' '[\0]' < a.txt | xargs -r0 /bin/bash -c 'command1 "$@"; command2 "$@";' ''

(-0 can be removed and the tr replaced with a redirect (or the file can be replaced with a null separated file instead). It is mainly in there since I mainly use xargs with find with -print0 output) (This might also be relevant on xargs versions without the -0 extension)

It is safe, since args will pass the parameters to the shell as an array when executing it. The shell (at least bash) would then pass them as an unaltered array to the other processes when all are obtained using ["$@"][1]

If you use ...| xargs -r0 -I{} bash -c 'f="{}"; command "$f";' '', the assignment will fail if the string contains double quotes. This is true for every variant using -i or -I. (Due to it being replaced into a string, you can always inject commands by inserting unexpected characters (like quotes, backticks or dollar signs) into the input data)

If the commands can only take one parameter at a time:

tr '[\n]' '[\0]' < a.txt | xargs -r0 -n1 /bin/bash -c 'command1 "$@"; command2 "$@";' ''

Or with somewhat less processes:

tr '[\n]' '[\0]' < a.txt | xargs -r0 /bin/bash -c 'for f in "$@"; do command1 "$f"; command2 "$f"; done;' ''

If you have GNU xargs or another with the -P extension and you want to run 32 processes in parallel, each with not more than 10 parameters for each command:

tr '[\n]' '[\0]' < a.txt | xargs -r0 -n10 -P32 /bin/bash -c 'command1 "$@"; command2 "$@";' ''

This should be robust against any special characters in the input. (If the input is null separated.) The tr version will get some invalid input if some of the lines contain newlines, but that is unavoidable with a newline separated file.

The blank first parameter for bash -c is due to this: (From the bash man page) (Thanks @clacke)

-c   If the -c option is present, then  commands  are  read  from  the  first  non-option  argument  com‐
     mand_string.   If there are arguments after the command_string, the first argument is assigned to $0
     and any remaining arguments are assigned to the positional parameters.  The assignment  to  $0  sets
     the name of the shell, which is used in warning and error messages.
4
  • This should work even with double quotes in filenames. That requires a shell that properly support "$@" Jul 12, 2018 at 12:06
  • 1
    You are missing the argv[0] argument to bash. bash -c 'command1 "$@"; command2 "$@";' arbitrarytextgoeshere
    – clacke
    Apr 5, 2019 at 6:05
  • 5
    This is not about what xargs does. bash with -c takes first (after the commands) one argument that will be the name of the process, then it takes the positional arguments. Try bash -c 'echo "$@" ' 1 2 3 4 and see what comes out.
    – clacke
    Apr 5, 2019 at 20:17
  • 1
    It's nice to have a safe version that doesn't get Bobby-Tabled. May 13, 2019 at 9:19
20

One thing I do is to add to .bashrc/.profile this function:

function each() {
    while read line; do
        for f in "$@"; do
            $f $line
        done
    done
}

then you can do things like

... | each command1 command2 "command3 has spaces"

which is less verbose than xargs or -exec. You could also modify the function to insert the value from the read at an arbitrary location in the commands to each, if you needed that behavior also.

2
  • 1
    Underrated answer, this is extremely handy Feb 13, 2020 at 18:24
  • Does not work correctly if input has two spaces in a row or *.
    – Ole Tange
    Feb 13, 2021 at 7:51
12

Another possible solution that works for me is something like -

cat a.txt | xargs bash -c 'command1 $@; command2 $@' bash

Note the 'bash' at the end - I assume it is passed as argv[0] to bash. Without it in this syntax the first parameter to each command is lost. It may be any word.

Example:

cat a.txt | xargs -n 5 bash -c 'echo -n `date +%Y%m%d-%H%M%S:` ; echo " data: " $@; echo "data again: " $@' bash
1
  • 7
    If you don't quote "$@", then you're string-splitting and glob-expanding the argument list. Jun 23, 2017 at 22:36
3

My current BKM for this is

... | xargs -n1 -I % perl -e 'system("echo 1 %"); system("echo 2 %");'

It is unfortunate that this uses perl, which is less likely to be installed than bash; but it handles more input that the accepted answer. (I welcome a ubiquitous version that does not rely on perl.)

@KeithThompson's suggestion of

 ... | xargs -I % sh -c 'command1; command2; ...'

is great - unless you have the shell comment character # in your input, in which case part of the first command and all of the second command will be truncated.

Hashes # can be quite common, if the input is derived from a filesystem listing, such as ls or find, and your editor creates temporary files with # in their name.

Example of the problem:

$ bash 1366 $>  /bin/ls | cat
#Makefile#
#README#
Makefile
README

Oops, here is the problem:

$ bash 1367 $>  ls | xargs -n1 -I % sh -i -c 'echo 1 %; echo 2 %'
1
1
1
1 Makefile
2 Makefile
1 README
2 README

Ahh, that's better:

$ bash 1368 $>  ls | xargs -n1 -I % perl -e 'system("echo 1 %"); system("echo 2 %");'
1 #Makefile#
2 #Makefile#
1 #README#
2 #README#
1 Makefile
2 Makefile
1 README
2 README
$ bash 1369 $>  
1
  • 6
    # problem can be easly solved using quotes: ls | xargs -I % sh -c 'echo 1 "%"; echo 2 "%"'
    – gpl
    Jul 14, 2016 at 16:42
2

Try this:

git config --global alias.all '!f() { find . -d -name ".git" | sed s/\\/\.git//g | xargs -P10 -I{} git --git-dir={}/.git --work-tree={} $1; }; f'

It runs ten threads in parallel and does what ever git command you want to all repos in the folder structure. No matter if the repo is one or n levels deep.

E.g: git all pull

2
  • 1
    Your example is very useful, but it's convoluted enough that an explanation would help. It appears that it doesn't answer the question about how to execute multiple commands with xargs. What your example does is git --git-dir=A1/.git --work-tree=A1 pull in your example where A1 is one of the repos it finds. The question was how to do something like ls -al {}; rm -f {} (i.e. two commands for each line given to xargs not one). Jan 31, 2021 at 4:37
  • 1
    My comment seems to be way out of place. It does not answer the original question at all. I was certain I posted this on a different question :D What my comment does is to prepare a git alias. The alias allows for running git commands on all repos located in sub-directories. @SteventheEasilyAmused, do you think I should remove the comment?
    – Erik Berg
    Feb 1, 2021 at 9:33
1

I have good idea to solve the problem. Only write a comman mcmd, then you can do

find . -type f | xargs -i mcmd echo {} @@ cat {} @pipe sed -n '1,3p'

The mcmd content as follows:

echo $* | sed -e 's/@@/\n/g' -e 's/@pipe/|/g' | csh
2
  • I applaud you for not settling for the sh -c solution which doesn't sit right with me. Jun 11, 2021 at 5:54
  • 2
    Uhhhh, it's not like csh is less awful than ... anything at all.
    – tripleee
    Apr 11, 2023 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.