Linux 文件描述符与重定向实战:从原理到 minishell 实现
Linux 文件描述符是内核管理 IO 资源的核心机制,通过非负整数索引进程打开的文件对象。重定向基于修改 fd 指向的文件对象实现,常用 close+open 或 dup2 系统调用。 fd 分配规则、默认流(0/1/2)及重定向原理,并通过 minishell 实战演示如何在子进程中利用 dup2 实现输入输出重定向,涵盖解析命令行、执行重定向及程序替换的关键步骤,帮助理解 Shell 底层 IO 逻辑。

Linux 文件描述符是内核管理 IO 资源的核心机制,通过非负整数索引进程打开的文件对象。重定向基于修改 fd 指向的文件对象实现,常用 close+open 或 dup2 系统调用。 fd 分配规则、默认流(0/1/2)及重定向原理,并通过 minishell 实战演示如何在子进程中利用 dup2 实现输入输出重定向,涵盖解析命令行、执行重定向及程序替换的关键步骤,帮助理解 Shell 底层 IO 逻辑。

文件描述符(fd)是 Linux IO 的核心概念,所有文件操作最终都通过文件描述符完成;而重定向(>、>>、<)则是基于文件描述符的经典应用,是 Shell 的核心功能之一。理解文件描述符的分配规则、重定向的底层原理,不仅能帮你搞懂 Linux IO 的本质,还能轻松实现自定义 Shell 的重定向功能。
文件描述符是 Linux 内核给打开的文件(广义文件,包括磁盘文件、键盘、显示器等)分配的非负整数,本质是进程 files_struct 结构体中文件指针数组的下标。通过这个下标,进程能快速找到对应的内核文件对象(struct file),从而完成 IO 操作。

Linux 进程启动时会默认打开 3 个文件描述符,对应 3 个标准流:
fd=0:标准输入(stdin),对应键盘;fd=1:标准输出(stdout),对应显示器;fd=2:标准错误(stderr),对应显示器。验证代码:
#include <stdio.h>
int main() {
// 打印标准流对应的文件描述符
printf("stdin: %d\n", stdin->_fileno); // 输出:stdin: 0
printf("stdout: %d\n", stdout->_fileno); // 输出:stdout: 1
printf("stderr: %d\n", stderr->_fileno); // 输出:stderr: 2
return 0;
}


代码示例:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
// 打印默认 fd
printf("stdin: %d, stdout: %d, stderr: %d\n", stdin->_fileno, stdout->_fileno, stderr->_fileno);
// 新建 3 个文件,观察 fd 分配
int fda = open("loga.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
int fdb = open("logb.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
int fdc = open("logc.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
printf("fda: %d, fdb: %d, fdc: %d\n", fda, fdb, fdc); // 输出:3,4,5
// 关闭 fda(fd=3),再新建文件,观察是否分配 3
close(fda);
int fdd = open("logd.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
printf("fdd: %d\n", fdd); // 输出:3
// 关闭所有 fd
close(fdb);
close(fdc);
close(fdd);
return 0;
}
运行结果说明:默认情况下,新打开文件的 fd 从 3 开始分配;关闭某个 fd 后,后续新文件会优先占用该空闲 fd。


open、read、write)直接操作文件描述符,是 IO 的底层接口;fopen、fread、fwrite)封装了系统调用,内部通过 FILE 结构体管理文件描述符(FILE->_fileno)和用户级缓冲区;fopen → open(返回 fd)→ FILE 结构体封装 fd → fwrite → write(通过 fd 操作文件)。
重定向的核心是修改文件描述符对应的文件对象。例如:
cat > file.txt):将 fd=1(stdout)原本指向的'显示器文件',改为指向'file.txt';cat < file.txt):将 fd=0(stdin)原本指向的'键盘文件',改为指向'file.txt';echo "hello" >> file.txt):将 fd=1 指向'file.txt',且写入时追加到文件末尾。#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
// 关闭 fd=1(stdout)
close(1);
// 打开文件,fd 会分配为 1(因为 1 是当前最小空闲 fd)
int fd = open("log.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
// 此时 printf、fprintf(stdout) 都会写入 log.txt
printf("hello printf\n");
fprintf(stdout, "hello fprintf\n");
close(fd);
return 0;
}
运行后,原本输出到显示器的内容会写入 log.txt,验证了输出重定向的本质。

dup2(oldfd, newfd) 函数会将 newfd 重定向到 oldfd 对应的文件,自动关闭 newfd(若已打开),是实现重定向的标准接口。

示例:用 dup2 实现输出重定向:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
// 打开文件,获取 fd(假设为 3)
int fd = open("log.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
// 将 fd=1(stdout)重定向到 fd 对应的文件
dup2(fd, 1);
printf("hello dup2\n");
fprintf(stdout, "hello stdout\n");
close(fd);
return 0;
}
dup2 无需手动关闭 newfd,更简洁可靠,是 Shell 实现重定向的首选接口。
补充一个输入的示例:
int main() {
int fda = open("loga.txt", O_RDONLY);
dup2(fda, 0);
int a = 0;
float f = 0.0f;
char c = 0;
scanf("%d %f %c", &a, &f, &c);
printf("%d, %f, %c\n", a, f, c);
close(fda);
return 0;
}

基于 myshell.c 代码,完善重定向解析和执行逻辑,实现 >(输出重定向)、>>(追加重定向)、<(输入重定向)功能。

>、>>、<)和目标文件名;dup2 修改 fd 对应的文件;(1)头文件(myshell.h)&& 主函数(main.c)
#pragma once
#include <stdio.h>
void bash();
#include "myshell.h"
int main() {
bash();
return 0;
}
(2)核心实现(myshell.c)
#include "myshell.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
// 提示符相关
static char username[32];
static char hostname[64];
static char cwd[256];
static char commandLine[256];
// 与命令行相关
static char* argv[64];
static int argc = 0;
static const char* sep = " ";
// 与退出码有关
static int lastCode = 0;
// 与环境变量相关,按道理来说是由 bash 来维护的,从系统配置文件读,但是我们这里直接从系统 bash 拷贝就行了
char** _environ;
static int envc = 0;
// 重定向相关
#define NoneRedir 0
#define InputRedir 1
#define OutputRedir 2
#define AppRedir 3
static int redir_type = NoneRedir;
static char* redir_filename = NULL;
#define CLEAR_LEFT_SPACE(pos) do { while (isspace(*pos)) pos++; } while (0)
static void InitEnv() {
extern char** environ; // 系统环境变量数组(以 NULL 结尾)
for (envc = 0; environ[envc]; envc++) {
_environ[envc] = environ[envc];
}
}
static void PrintAllEnv() {
int i = 0;
for (; _environ[i]; i++) {
printf("%s\n", _environ[i]);
}
}
static void AddEnv(const char* val) // argv[1]
{
_environ[envc] = (char*)malloc(strlen(val) + 1);
strcpy(_environ[envc], val);
_environ[++envc] = NULL;
}
static void GetUserName() {
char* _username = getenv("USER");
strcpy(username, (_username ? _username : "None"));
}
static void GetHostName() {
char* _hostname = getenv("HOSTNAME");
strcpy(hostname, (_hostname ? _hostname : "None"));
}
static void GetCmd() {
char _cwd[256];
getcwd(_cwd, sizeof(_cwd));
if (strcmp(_cwd, "/") == 0) {
strcpy(cwd, _cwd);
} else {
int end = strlen(_cwd) - 1;
while (end >= 0) {
if (_cwd[end] == '/') {
strcpy(cwd, &_cwd[end + 1]);
break;
}
end--;
}
}
}
static void PrintPromt() {
GetUserName();
GetHostName();
GetCmd();
printf("[%s@%s %s]# ", username, hostname, cwd);
fflush(stdout);
}
static void GetCommandLine() {
if (fgets(commandLine, sizeof(commandLine), stdin) != NULL) {
commandLine[strlen(commandLine) - 1] = 0;
}
}
// 1. yes
// 0. no, 普通命令,让后续的执行
int CheckBuiltinAndExcute() {
int ret = 0;
if (strcmp(argv[0], "cd") == 0) {
ret = 1;
if (argc == 2) // 后面至少需要跟个东西
{
chdir(argv[1]);
}
} else if (strcmp(argv[0], "echo") == 0) {
ret = 1;
if (argc == 2) {
if (argv[1][0] == '$') {
if (strcmp(argv[1], "?$?") == 0) {
printf("%d\n", lastCode);
lastCode = 0;
} else {
// env
}
} else {
printf("%s\n", argv[1]);
}
}
} else if (strcmp(argv[0], "env") == 0) {
ret = 1;
PrintAllEnv();
} else if (strcmp(argv[0], "export") == 0) {
ret = 1;
if (argc == 2) {
AddEnv(argv[1]);
}
}
return ret;
}
void Excute() {
pid_t id = fork();
if (id < 0) {
perror("fork");
return;
} else if (id == 0) {
// 子进程
// 程序替换
// 要不要重定向,怎么重定向
if (redir_type == InputRedir) {
int fd = open(redir_filename, O_RDONLY);
(void)fd;
dup2(fd, 0);
} else if (redir_type == OutputRedir) {
int fd = open(redir_filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
(void)fd;
dup2(fd, 1);
} else if (redir_type == AppRedir) {
int fd = open(redir_filename, O_CREAT | O_WRONLY | O_APPEND, 0666);
(void)fd;
dup2(fd, 1);
} else {
// do nothing
}
execvp(argv[0], argv);
exit(1);
} else {
// 父进程
int status = 0;
pid_t rid = waitpid(id, &status, 0);
(void)rid;
lastCode = WEXITSTATUS(status);
}
}
static void ParseCommandLine() {
// 清空
argc = 0;
memset(argv, 0, sizeof(argv));
// 判空
if (strlen(commandLine) == 0)
return;
// 分割
argv[argc] = strtok(commandLine, sep);
while ((argv[++argc] = strtok(NULL, sep)));
}
void Redir() {
// 核心目标
// "ls -a -l >> > < filename"
// redir_filename = filename
// redir_type = InputRedir
char* start = commandLine;
char* end = commandLine + strlen(commandLine);
while (start < end) {
// > >> <
if (*start == '>') {
if (*(start + 1) == '>') {
// 追加重定向
redir_type = AppRedir;
*start = 0;
start += 2;
CLEAR_LEFT_SPACE(start);
redir_filename = start;
break;
} else {
// 输出重定向
redir_type = OutputRedir;
*start = '\0';
start++;
CLEAR_LEFT_SPACE(start);
redir_filename = start;
break;
}
} else if (*start == '<') {
// 输入重定向
redir_type = InputRedir;
*start = '\0';
start++;
CLEAR_LEFT_SPACE(start);
redir_filename = start;
break;
} else {
start++;
}
}
}
void bash() {
// 环境变量相关,方便实现通过声明 (_environ) 就能直接用环境变量
static char* env[64];
_environ = env;
// 除此以外我们还可以通过一个数组存储本地变量
// 以及可以通过一个来存储别名…
// 初始化读取环境变量
InitEnv();
while (1) {
// 每次开始前重置一下重定向文件和状态
redir_type = NoneRedir;
redir_filename = NULL;
// 第一步:输出提示命令行
PrintPromt();
// 第二步:等待用户输入,获取用户输入
GetCommandLine();
// "ls -a -l > filename" -> "ls -a -l" "filename" redir_type
// 2.1 Redir();
Redir();
// 第三步:解析字符串,"ls -a -l" -> "ls" "-a" "-l"
ParseCommandLine();
if (argc == 0)
continue;
// 第四步:有些命令,cd echo env 等等不应该让子进程执行
// 而是让父进程自己执行,这些是内建命令。bash 内部的函数
if (CheckBuiltinAndExcute())
continue;
// 第五步:执行命令
Excute();
}
}
关键注意点:
execvp 会保留子进程的 fd 映射,替换后的程序会沿用重定向后的 fd;dup2 后需关闭原 fd(如打开的文件 fd),避免 fd 泄漏;fflush(stdout)。文件描述符是 Linux IO 的底层基石,重定向是其最经典的应用之一。本文从 fd 的本质、分配规则,到重定向原理,再到 minishell 的实战实现,全程用代码验证,让你不仅'知其然',更'知其所以然'。基于这个基础,还可以扩展管道(|)、后台运行(&)等 Shell 高级功能。管道的本质是'将前一个命令的 stdout 重定向到管道,后一个命令的 stdin 重定向到管道',核心思路与本文重定向一致。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online
将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online