dup2
dup2 is a POSIX system call and C library function that creates a duplicate of an existing file descriptor oldfd and places it in the descriptor number newfd. If newfd is currently open, dup2 closes it before duplicating oldfd. If oldfd and newfd are the same, the call succeeds and simply returns newfd.
After successful completion, both descriptors refer to the same open file description; the file offset and
Return value and errors: On success, dup2 returns newfd. On error, it returns -1 and sets errno.
Usage and portability: dup2 is widely available on Unix-like systems and is often used to redirect standard