Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash: multiple files after standard input redirection

Hey I have been researching this for a while and I was wondering if someone can explain the mechanism of this particular feature on the bash shell.

Lets say we have 3 files: test1.txt, test2.txt, and test3.txt.

Contents of each file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

test1.txt = "foo"

test2.txt = "bar"

test3.txt = "hello world"

if we run the following command on bash shell: cat < test1.txt test2.txt test3.txt we would get:

bar 

hello world

My question is why the contents of test1.txt ignored in this case, and if anyone has any good sources I can read up on this particular feature.

>Solution :

The input redirection is processed before identifying arguments. The command you show, for example, is equivalent to each of

  • < test1.txt cat test2.txt test3.txt
  • cat test2.txt < test1.txt test3.txt
  • cat test2.txt test3.txt < test1.txt

In any case, you are left with cat‘s standard input being test1.txt, and it receiving two command-line arguments test2.txt and test3.txt.

cat, however, only reads from its standard input if it has no arguments naming input files. If you want to read from both standard input and named files, use - as the "name" of standard input.

# Same result as cat test1.txt test2.txt test3.txt
cat - test2.txt test3.txt < test1.txt
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading