1

I have a txt file, each line is a command. I would like to execute each. I do this on Ubuntu:

cat file_with_commands.txt | xargs -P10 -I {} bash -c '{}'

and it works. The -P10 flag makes sure up to 10 processes can be run at once. The same on Mac results in:

bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found

bash --version tells me: GNU bash, version 5.2.0(1)-release (aarch64-apple-darwin21.6.0). The same in Ubuntu gives: GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)

How can I execute each line of file_with_commands.txt on Mac?

I also tried cat file_with_commands.txt | xargs -P10 -I {} bash -c "{}" and cat file_with_commands.txt | xargs -P10 -I {} bash -c {}, results are the same.

On Mac, xargs --version gives:

xargs: illegal option -- -
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]
             [-J replstr] [-L number] [-n number [-x]] [-P maxprocs]
             [-s size] [utility [argument ...]]

cat file_with_commands.txt | xargs -t -P 10 -I {} bash -c '{}' gives:

bash -c {}
bash -c {}
bash -c {}
bash -c {}
bash -c {}
bash -c {}
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found
bash: line 1: {}: command not found

cat file_with_commands.txt | xargs -t -I {} echo {} returns:

echo {}
{}
echo {}
{}
echo {}
{}
echo {}
{}
echo {}
{}
echo {}
{}

Interestingly, this weird behaviour is only present if the line is long. If the line is short enough, I get the line itself when echoing it, not {}. 254 character long strings seem to be short enough, longer ones seem to be too long.

zabop
  • 6,750
  • 3
  • 39
  • 84
  • 1
    but.... why not just `bash file_with_commands.txt`? `bash: line 1: {}: command not found` Are you 100% sure, that you typed `-I {}` and not `-I ﹛ ﹜` and not `-I { }` or anything else? – KamilCuk Apr 11 '23 at 12:34
  • Then post real world use case. What is the output of `xargs --version`? What does `xargs -t ....` output? – KamilCuk Apr 11 '23 at 12:45
  • Question appended with these details! – zabop Apr 11 '23 at 12:51
  • `xargs -t .... does not output anything within a minute of trying.` Sorry, I meant, `cat file_with_commands.txt | xargs -t -P 10 -I {} bash -c '{}'` – KamilCuk Apr 11 '23 at 12:52
  • Updated! I didn't know about the -t option, but it seems like a good way to locate the problem a bit more. It seems like bash is not getting the command it is supposed to execute from xargs. – zabop Apr 11 '23 at 12:56
  • Maybe duplicate of this: https://stackoverflow.com/questions/36224777/xargs-command-length-limits. – zabop Apr 11 '23 at 13:49
  • https://stackoverflow.com/questions/6958689/running-multiple-commands-with-xargs has some variations, though it's about a different problem. – tripleee Apr 11 '23 at 17:32

0 Answers0