Bash Redirection Fun With Descriptors

Bash Redirection: The Basics

Output Redirection

echo "hello world" 1> foo.txt

No Clobber and Forced Redirection

Input Redirection

read input < foo.txt && echo "${input}"

Assigning file descriptors to files

~ : exec 5> bar.txt
~ : echo "bye, world" >& 5
~ : cat bar.txt
bye, world
~ : exec 6< bar.txt
~ : read -n input <& 6 && echo "${input}"
bye, world

Gotcha

Closing File Descriptors

~ : exec 6< bar.txt
~ : read input <& 6 && echo "${input}"
bye, world
~ : exec 6<&-
~ : read input <& 6 && echo "${input}"
bash: 6: bad file descriptor
bash-5.0$ echo "hello world" > foo.txt
bash-5.0$ cat foo.txt
hello world
bash-5.0$ exec 1<&-
bash-5.0$ cat foo.txt
cat: stdout: Bad file descriptor
bash-5.0$ exec 0<&-
bash-5.0$ exit

Moving File Descriptors

Moving Input File Descriptors

exec X<&Y-
bash-5.0$ echo "hello world" > foo.txt
bash-5.0$ exec 6< foo.txt
bash-5.0$ read input <& 6 && echo "${input}"
hello world
bash-5.0$ exec 7<&6-
bash-5.0$ read input <& 6 && echo "${input}"
bash: 6: Bad file descriptor
bash-5.0$ read input <& 7 && echo "${input}"
hello world

Moving Output File Descriptors

exec X>&Y-
bash-5.0$ touch foo.txt
bash-5.0$ exec 6> foo.txt
bash-5.0$ echo "hello, world" >& 6
bash-5.0$ cat foo.txt
hello, world
bash-5.0$ echo "bye, world" >& 6
bash-5.0$ cat foo.txt
hello, world
bye, world
bash-5.0$ exec 8>&6-
bash-5.0$ echo "hello again, world" >& 6
bash: 6: Bad file descriptor
bash-5.0$ echo "hello again, world" >& 8
bash-5.0$ cat foo.txt
hello, world
bye, world
hello again, world

Using a Single File Descriptor to Read And Write

exec X<>file
bash-5.0$ touch foo.txt
bash-5.0$ exec 7<>foo.txt
bash-5.0$ echo "Hello, World!" >&7
bash-5.0$ echo "Hello, New World!" >&7
bash-5.0$ cat foo.txt
Hello, World!
Hello, New World!
bash-5.0$
bash-5.0$
bash-5.0$ read input <&7 && echo "${input}"
bash-5.0$
bash-5.0$ touch foo.txt
bash-5.0$ exec 7<>foo.txt
bash-5.0$ echo "Hello, World!" >&7
bash-5.0$ echo "Hello, New World!" >&7
bash-5.0$ cat foo.txt
Hello, World!
Hello, New World!
bash-5.0$
bash-5.0$
bash-5.0$ read input <&7 && echo "${input}"
bash-5.0$
bash-5.0$ exec 7<>foo.txt
bash-5.0$ read input <&7 && echo "${input}"
Hello, World!
bash-5.0$ touch foo.txt
bash-5.0$ exec 7<>foo.txt
bash-5.0$ echo "Hello, World!" >&7
bash-5.0$ echo "Hello, New World!" >&7
bash-5.0$ cat foo.txt
Hello, World!
Hello, New World!
bash-5.0$ read input <&7 && echo "${input}"
bash-5.0$
bash-5.0$ exec 7<>foo.txt
bash-5.0$ read input <&7 && echo "${input}"
Hello, World!
bash-5.0$ cat foo.txt
Hello, World!
Hello, New World!
bash-5.0$ echo "Bye, World!" >&7
bash-5.0$ cat foo.txt
Hello, World!
Bye, World!
orld!
bash-5.0$

Conclusion

--

--

@copyconstruct on Twitter. views expressed on this blog are solely mine, not those of present or past employers.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cindy Sridharan

@copyconstruct on Twitter. views expressed on this blog are solely mine, not those of present or past employers.