cd 程序是否符合 POSIX 标准?

Nik*_* R. 4 shell c cd-command posix

通配和外壳扩展是一回事吗?我正在通过编写自定义 shell 来学习 C,我也在学习 POSIX。现在我想知道是否是 POSIX 合规性cd -让您回归,这是否~意味着主目录?因为并非所有 shell 都可以做到这一点,而且我不知道是否需要 cd 命令的最小 POSIX 合规性。我使用的实现是

int do_cd(int argc, const char **argv) {
    const char *path;
    if (argc > 1) {
        path = argv[1];
    }
    else {
        path = getenv("HOME");
        if (path == NULL) {
            fprintf(stderr, "No HOME environment variable\n");
            return 1;
        }
    }
    if (chdir(path) < 0) {
        perror(path);
        return 1;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*mer 8

波浪号扩展是 shell 命令处理的一部分,而不是cd. cd将已经扩展的路径视为其参数。

POSIX要求cd -等效于cd "$OLDPWD" && pwd. OLDPWD 必须cdPWD在运行命令时if设置

  • @Programmer400,...那么`sash` 不是符合POSIX 的shell。 (3认同)