The file descriptor is an integer associated with the input and output of the file, which is used to track open files, and the file descriptor 0,1,2 is reserved by the system.
0--stdin (standard input)
1--stdout (standard output)
2--stderr (standard error)
echo a1>CP A1 A2; CP Cat A *cat: a1:permission denieda1a1
Direct 0 to Std.txt, 2 to Err.txt
$ cat A * >std.txt 2>err.txt
Both 0 and 1 are directed to the OUT.txt
$ cat A * >out.txt 2>&1.txt
Tee command
Redirects the stdin to text and prints it on the screen.
Cat a*| Tee OUT.txt Cat cat out.txt a1a1
Note: OUT.txt does not contain "cat: A1:permission denied" because it can only be read from stdin.
The default tee command overwrites the file, but provides a-a option to append.
Cat a*| Tee-a out.txt
Text block inside the redirect script
#/bin//nolog <<eof >log.txtconn Scott/Tigerselectcat; Commit;exiteof
The content between <<eof >log.txt and EOF will be redirected to Log.txt as Stdin,stdout.
Custom file descriptors
- Create a file descriptor to write to
4>echo newline >&4cat output.txt newline
- Creates a file descriptor for reading
echo Hello >
3<input.txtcat <&3Hello
Shell Scripting Learning Summary--file descriptors and redirects