shorthand for feeding contents of multiple files to the stdin of a script
Solution 1
You can use cat
and a pipe:
cat file1 file2 file3 ... fileN | ./script
Your example, using a pipe, and no temp file:
join file1.txt file2.txt | ./script
Solution 2
If you don't want to use a pipe, you can use input redirection with process substitution:
./script <(cat file1 file2)
Solution 3
To add on @Jonah Braun's answer, if you ever need to add process output into your script as well, i.e. your file might not be on your disk but accessed via URL
using curl
or a similar tool.
Something like this could be used to get stdout
of multiple processes and use them in a script via stdin
This will be the script to handle input
Contents of multi-input.sh
:
#!/usr/bin/env bash
while read line; do
echo $line
done
Now test it:
$ ./multi-input.sh < <(cat <(echo process 1) <(echo process 2) <(echo process 3))
Output:
process 1
process 2
process 3
<()
turns process into a virtual file
using fd
if you will, so <
is needed to read it. cat
itself doesn't need it because it does what it does, concatenates files, virtual or real.
Related videos on Youtube
![0x4B1D](https://i.stack.imgur.com/cGFWJ.jpg?s=256&g=1)
0x4B1D
Updated on September 18, 2022Comments
-
0x4B1D almost 2 years
Let's say I have a script called
script
, that reads fromstdin
and spits out some results to the screen.If I wanted to feed it contents of one file, I would have typed:
$ ./script < file1.txt
But what if I want to feed the contents of the multiple files to the script the same way, is it at all possible? The best I came up with so far was:
cat file1.txt file2.txt > combined.txt && ./script < combined.txt
Which uses two commands and creates a temp file. Is there a way to do the same thing but bypassing creating the combined file?
-
don_crissti over 6 yearsSwitch to
zsh
and you'll be able to runcmd <file1 <file2 ... <fileN
;)
-
-
Angel Todorov almost 13 yearsUseful use of cat award!
-
mythofechelon over 4 yearsDo you know of any reason where the script will only receive / output nothing (literally
""
)?