Application 2019-05-08

Redirect with exec Command

Master shell exec command for dynamic file descriptor redirection, input/output control, and terminal manipulation.

Read in: ja
Redirect with exec Command

Overview

The exec command replaces the current process with a command, but when used without arguments, it allows for dynamic changes to redirection.

This was prompted by the code that appeared when confirming at the prompt during direct push to master:

#!/bin/sh
eval < /dev/tty
read ANSWER

I didn't quite understand it, so I looked it up.

Usage

#!/bin/sh
echo "Output to stdout" // Standard input
exec > redirect.txt // Change file descriptor
echo "Output to file" // Output to file

The code that appeared during direct push to master is:

#!/bin/sh
exec < /dev/tty
read ANSWER

This passes the input from the current terminal (/dev/tty) to the standard input of the exec command. (It's a bit confusing...)

Thoughts

It seems useful when you can't read from standard input for some reason.

References

Tags: bash shell script exec
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles