Following are some redirection options in bash that are not widely used but are very useful
- To redirect both stdout and stdin to a file in bash
echo tan &> filename
OR
echo tan > filename 2>&1
- To redirect stdout of a command to stderr
echo tan 2>&1
- Following command will open a file for reading and writing and assign the interger file description N to it. If the file does not exist then it will be created
9<>filename
Following is an example of how you can use this
exec 9<>temp # open file temp and assign file descriptor 9 to it
echo aman >&9 # Write aman to the file
exec 9>&- # Close the file descriptor Now read the file and it will contain string aman
cat temp
Before closing the file descriptor, you can see it opened
cd /proc/$$/fd
Closing File Descriptors
- To redirect both stdout and stdin to a file in bash
echo tan &> filename
OR
echo tan > filename 2>&1
- To redirect stdout of a command to stderr
echo tan 2>&1
- Following command will open a file for reading and writing and assign the interger file description N to it. If the file does not exist then it will be created
9<>filename
Following is an example of how you can use this
exec 9<>temp # open file temp and assign file descriptor 9 to it
echo aman >&9 # Write aman to the file
exec 9>&- # Close the file descriptor Now read the file and it will contain string aman
cat temp
Before closing the file descriptor, you can see it opened
cd /proc/$$/fd
Closing File Descriptors
- n<&-
-
Close input file descriptor n.
- 0<&-, <&-
-
Close stdin.
- n>&-
-
Close output file descriptor n.
- 1>&-, >&-
- Close stdout
No comments:
Post a Comment