3

For instance, if I'd like to reference the output of the previous command once, I can use the command below:

ls *.txt | xargs -I % ls -l %

But how to reference the output twice? Like how can I implement something like:

ls *.txt | xargs -I % 'some command' % > %

PS: I know how to do it in shell script, but I just want a simpler way to do it.

2 Answers 2

4

You can pass this argument to bash -c:

ls *.txt | xargs -I % bash -c 'ls -l "$1" > "out.$1"' - %
Sign up to request clarification or add additional context in comments.

Comments

1

You can lookup up 'tpipe' on SO; it will also lead you to 'pee' (which is not a good search term elsewhere on the internet). Basically, they're variants of the tee command which write to multiple processes instead of writing to files like the tee command does.

However, with Bash, you can use Process Substitution:

ls *.txt | tee >(cmd1) >(cmd2)

This will write the input to tee to each of the commands cmd1 and cmd2.

You can arrange to lose standard output in at least two different ways:

ls *.txt | tee >(cmd1) >(cmd2) >/dev/null
ls *.txt | tee >(cmd1) | cmd2

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.