Skip to content

Instantly share code, notes, and snippets.

@AlexAtkinson
Created December 5, 2025 02:36
Show Gist options
  • Select an option

  • Save AlexAtkinson/d5d92bb2b21a5e7c14b089f225fc25bd to your computer and use it in GitHub Desktop.

Select an option

Save AlexAtkinson/d5d92bb2b21a5e7c14b089f225fc25bd to your computer and use it in GitHub Desktop.
Linux Redirects

Redirects

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment