Change input direction in the shell
Change input direction in the shell
To apply input redirection in the shell, simply append the desired command after your script. For example, to redirect standard input to a file, you can run `input.c > output.txt`. This captures everything typed into the terminal and saves it to the specified location.
In unix-like environments everything functions as a file, even stdout. This implies your program's output appears in stdout like a standard text file. The > symbol copies whatever exists on its left into the right (and vice versa for <)—so on the left side of ls -l > foo.txt you find the data stdout has produced, while on the right it becomes a regular text file containing that output. In another scenario, the contents from chicken.txt are being directed to stdin, which your program then reads. Keep in mind the precise behavior will vary by shell.
When redirecting input, you can still type normally; the redirected version is just an alternative way to get input without re-entering it. Running the program in this mode saves time compared to manual input.
It hinges on your program goals. Using a generic tool that reads standard input fits the "everything is a file" mindset, letting you link multiple scripts that simply ingest stdin without concern for its origin. Another handy method is piping ls | grep Documents, which lets you feed the output of one command into another (grep) seamlessly.