command 1: cat file1 > file2 (success)
command 2: file2 < cat file1 (cat : No such file or directory)
I just wanted to know the general syntax of output and input redirections.
If redirection can happen to file or stream, why command 2 fails?
Asked
Active
Viewed 1,121 times
1
Prem
- 115
- 5
-
command 2 is not valid: file2 is the "executable" and your try to send it cat as input. What do you actual want to do? – cylgalad Feb 23 '18 at 08:27
-
1Also basic shell stuff: `>` sends output to a file, `<` uses a file as input. – cylgalad Feb 23 '18 at 08:33
-
@cylgalad Got it now. Please put the second comment as answer. – Prem Feb 23 '18 at 08:37
-
@cylagad The command is not invalid (if file2 is a script), but it's not the same as the first one. – Kusalananda Feb 23 '18 at 09:12
1 Answers
2
The redirection in
cat file1 >file2
is an output redirection, while < specifies an input redirection.
The line
file2 < cat file1
is the same as
file2 file1 <cat
(it does not matter much where the redirection actually occurs as it's handled by the shell in its own parsing step and then removed from the actual command) which means "run file2 with file1 as argument, and redirect the standard input from the file cat".
The error comes from the shell trying to open cat as a file in the current directory. The error occurs before the shell tries to run the command file2.
Kusalananda
- 320,670
- 36
- 633
- 936