Can this usage of xargs argument enumaration be optimized better? The aim is to inject single argument in the middle of the actual command.
I do:
echo {1..3} | xargs -I{} sh -c 'for i in {};do echo line $i here;done'
or
echo {1..3} | for i in $(xargs -n1);do echo line $i here; done
I get:
line 1 here line 2 here line 3 here
which is what I need but I wondered if loop and temporary variable could be avoided?
Advertisement
Answer
Try without xargs
. For most situations xargs
is overkill.
Depending on what you really want you can choose a solution like
# Normally you want to avoid for and use while, but here you want the things splitted. for i in $(echo {1 2 3} );do echo line $i here; done # When you want 1 line turned into three, `tr` can help echo {1..3} | tr " " "n" | sed 's/.*/line & here/' # printf will repeat itself when there are parameters left printf "line %s heren" $(echo {1..3}) # Using the printf feature you can avoid the echo printf "line %s heren" {1..3}