Redirect operations are quite involved. Here are a few of the highlights:
- The files: '/dev/std{in,out,err}', are symlinks to file descriptors.
- File descriptors (aka file handles) are numbers that identify open files.
- A file is an object that stores data, information, settings, or commands.
- The numbers commonly seen in the redirect '2>&1' are file descriptors.
- Running 'file /dev/std*' outputs: /dev/stdin: symbolic link to /proc/self/fd/0 /dev/stdout: symbolic link to /proc/self/fd/1 /dev/stderr: symbolic link to /proc/self/fd/2
Note that the FDs exist in '/proc/self/fd/'. The '/proc/self' is a symlink to a processe's own /proc/[pid] directory.
Run the following exercise in a terminal one command at a time. Don't worry when your terminal stops displaying what's being typed.
# FD 1,2 to dev/null
exec 4>&1
exec 5>&1 >/dev/null
exec 6>&2 >/dev/null
exec >/dev/null 2>&1
echo bye
bad-cmd
echo "super echo" &>super.out
exec 1>&5
exec 2>&6
echo "hi again"
cat super.out
bad-cmd