Skip to content

Instantly share code, notes, and snippets.

@estysdesu
Last active July 21, 2019 08:54
Show Gist options
  • Save estysdesu/4747b9ab2003c08d15c77a95257315d5 to your computer and use it in GitHub Desktop.
Save estysdesu/4747b9ab2003c08d15c77a95257315d5 to your computer and use it in GitHub Desktop.
[Shell: file descriptors and file directors] #fd #file #descriptors #sh #read
# https://thoughtbot.com/blog/input-output-redirection-in-the-shell
# http://mywiki.wooledge.org/BashSheet#Streams
# http://www.learnlinux.org.za/courses/build/shell-scripting/ch01s04.html
##### fd (FILE DESCRIPTORS) #####
# 0 == stdin
# 1 == stdout
# 2 == stderr
# & == pointer (`2>&1`)
# &> == `2> <file> 1> <file>` (file direction)
##### >/>>/< (FILE DIRECTION) #####
# <1-cmd> <2-fd><3-direction> <4-file> # if no fd then assumed 1 (stdout) for out or 0 (stdin) for in
<cmd-w/-output> > <file> # write command's output (stdout) to file
<cmd-w/-output> >> <file> # append command's output (stdout) to file
<cmd-w/-input> < <file> # use file as command's input (stdin)
<cmd-w/-output> > &2 # write to output (stdout) to &2 (stderr) -- &2 is the file
<cmd-w/-output> 2> <file> # write command's error (sterr) to file
<cmd-w/-output> > <file-out> 2> <file-err> # stdout and stderr to different files
##### | (PIPING) #####
# similar to file direction except to another command
<cmd-w/-output> | <cmd-w/-input>
<cmd-w/-output> | <cmd-w/-input-and-output> | <cmd-w/-input>
##### stdout & stderr TO /dev/null (MUTE/NULLIFY) #####
# https://unix.stackexchange.com/questions/119648/redirecting-to-dev-null
<cmd-w/-output> > /dev/null 2>&1 # write command's output (stdout) to null and then redirect stderr to stdout (which points to null)
<cmd-w/-output> &> /dev/null # identical to above
##### read (READ DATA FROM stdin OR fd)
# -a: array
# -d: delimiter
x=`read -d' ' -a < <file>` # read a file into an array indexed with x[<num>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment