I have a txt file, each line is a bash 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 echo
ing it, not {}
. 254 character long strings seem to be short enough, longer ones seem to be too long.