为什么C不推出新的shell?

Why will C not launch a new shell?

我有一小块 C 代码。

#include<stdio.h>

int main()
{
    char *name[2];

    name[0] = '/bin/sh';
    name[1] = NULL;
    execve(name[0], name, NULL);
}

我有一个符号链接 sh -> zsh。当我 运行 程序没有任何反应,我保持原样 shell。我知道我不在新的 shell 中,因为当终端退出时我就退出了。如果我 运行 /bin/sh 我得到另一个 shell。是我的代码有误还是某种安全措施不允许我这样做?

用警告编译它会立即显示问题所在:

gcc -g t.c
t.c: In function ‘main’:
t.c:7:15: warning: character constant too long for its type
    7 |     name[0] = '/bin/sh';
      |               ^~~~~~~~~
t.c:7:13: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
    7 |     name[0] = '/bin/sh';
      |             ^
t.c:9:5: warning: implicit declaration of function ‘execve’ [-Wimplicit-function-declaration]
    9 |     execve(name[0], name, NULL);
      |     ^~~~~~

当然这个程序不行。您想要的字符串文字是 "/bin/sh"(您在 C 中使用的引号 很重要 )。