How can I make the second echo to echo out test in this example as well:
echo test | xargs -I {} echo {} && echo {}
How can I make the second echo to echo out test in this example as well:
echo test | xargs -I {} echo {} && echo {}
Just write {}
two times in your command. The following would work:
$ echo test | xargs -I {} echo {} {}
test test
Your problem is how the commands are nested. Lets look at this:
echo test | xargs -I {} echo {} && echo {}
bash will execute echo test | xargs -I {} echo {}
. If it runs successfully, echo {}
is executed. To change the nesting, you could do something like this:
echo test | xargs -I {} sh -c "echo {} && echo {}"
However, you could get trouble because the approach might be prone to code injection. When "test" is substituted with shell code, it gets executed. Therefore, you should probably pass the input to the nested shell with arguments.
echo test | xargs -I {} sh -c 'echo "$1" && echo "$1"' sh {}
$(rm -f *)
. It's better to do xargs -I {} sh -c 'echo "$1" && echo "$1"' sh {}
– Kusalananda
May 07 '19 at 20:04
echo
then yeah just use echo {} {}
but I think OP did that as a easy demonstration. In our case, we're redirecting the output to a file and using sh -c
did the trick perfectly. Thanks!
– Joshua Pinter
Jun 17 '21 at 19:14
Another option is to use -i
flag, which is the same as -I{}
(it implies that the replacement is given with {}
):
$ echo test | xargs -i echo {} {}
This option is deprecated; use -I instead.
has been added to the manual man xargs
.
– Jonathan Komar
Mar 25 '19 at 17:29
For me only the lower case works. I had hundreds of images in a directory and wanted to get them sources into a list. Upper case i -I
option did not work for me. Only the lower case. Probably due to version differences.
These images all had names like Daniel_(somenumber).jpg
.
This syntax has worked :
ls -l | tr -s ' ' ':'| cut -d: -f9 | xargs -i echo "img src='"{}"'alt='{}'"
Returns:
src='Daniel_248.jpg' alt='Daniel_248.jpg'
...
Linux ver 4.14.96-hw+ #80 SMP x86_64 GNU/Linux
"
) missing at the end of the echo
expression... FYI on macOS, I resorted to this simpler command: ls -1 | xargs -I {} echo "img src='{}' alt='{}'"
– maxxyme
Apr 07 '22 at 11:53
var=test & echo $var ...
– alexus Mar 02 '16 at 14:38