When I run the command type type
, this gives me a result to standard output.
So, now I try this command:
type type > abc.txt
It redirects the standard output to abc.txt
. So, if this command executes on the same process (bash itself), then file descriptor 1 points to abc.txt
.
After that, whenever I use run a command, these results should go to file abc.txt
, because file descriptor 1 points to abc.txt
.
However, results always go to standard output. Does this mean that shell built-ins run under a forked process?
Best Answer
Output redirection
File descriptor
1
representsstdout
, the standard output stream. When output redirection is used intype type > abc.txt
, the shell opens the fileabc.txt
for writing and the file descriptor1
is modified so that it points to the open file instead of the terminal device.However, this redirection only applies to the current command being executed so this does not imply that the command executes in a forked process (or subshell).
Persistent redirection
If you wanted the redirection to persist, you could use the
exec
shell builtin to modify the file descriptors, e.g., to redirect standard output for successive commands, run the following command.Be careful running this as your shell session will be hard to use if all command output is being redirected to a file instead of your terminal device. You can restore the
stdout
file descriptor to the terminal output device by redirecting it to the same device pointed to bystderr
(file descriptor2
):Related resources
For more details, see: