Skip to content

Instantly share code, notes, and snippets.

@BonfaceKilz
Last active January 1, 2017 14:47
Show Gist options
  • Save BonfaceKilz/860c6edfe1764e5ae5f2d301efa4674a to your computer and use it in GitHub Desktop.
Save BonfaceKilz/860c6edfe1764e5ae5f2d301efa4674a to your computer and use it in GitHub Desktop.
File Descriptors in bash- a quick primer with file descriptors in bash

File descriptor copying
[x]>&y, [x]<&y
Make FD x write to/ read from FD y's stream

Appending file redirection
[x]>>file
Make FD x= append to end of file

Redirecting standard output and standard error
&>file
Make both FD 1 and FD 2 write to file
This does the same operation as
>file 2&1 but is more concise
You can append append rather than truncate by:
&>>file

Here Documents

<<.[=]delimiter
  here-document
  delimiter

Make FD 0 (standard input) read from the string between the delimiters

Here strings
<<<string
Make FD 0(standard input) read from the string

Closing file descriptors
x>&-, x<&-
close FD x

Moving file descriptors
[x]>&y-, [x]<&y-
Replace FD x with FD y

Reading and Writing with a file descriptor
[x]<>file
Open FD x for both reading and writing to file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment