cat concatenates files or stdin and redirects it to stdout.
$ cat file1 > file5 file2 file3 file4
concatenates file1, file2, file3 and file 4 and writes it to file5.
$ cat file1 > file5 < file2 file3 file4
concatenates file1, file3 and file4 (not file2) and writes to file5
Please explain these outputs
Examples of what happens:
~/test$ echo "this is file 1"> file1
~/test$ echo "this is file 2"> file2
~/test$ echo "this is file 3"> file3
~/test$ echo "this is file 4">file4
~/test$ cat file1 > file5 file2 file3 file4
~/test$ cat file5
this is file 1
this is file 2
this is file 3
this is file 4
~/test$ cat file1 > file5 < file2 file3 file4
~/test$ cat file5
this is file 1
this is file 3
this is file 4
This is less about how
catworks, and more about shell redirection. The shell processes the command line before it runs the program. It’s easier to see if you push all the io redirection to the end of the command. The first becomes:The shell then changes the output of cat from the terminal to file5. This is completely independent of cat.
The second command is then
This this changes the standard input from the keyboard to
file2and the output, like before, to file5. In this instance, because there are files specified on the command line, cat ignores the standard input, reading only from the 1, 3, and 4. The-argument tells cat to read from standard input, soWould output the contents of files 1-4 to file 5.