【发布时间】:2019-02-24 09:04:44
【问题描述】:
以下代码成功列出了 Ubuntu bash 和 MacOS bash 上当前目录的内容。
int main() {
char* args[3];
args[0] = "ls";
args[1] = NULL;
args[2] = NULL;
execvp(args[0], args);
return 0;
}
以下代码在 Ubuntu bash 上不打印任何内容,但在 MacOS bash 上打印 ls is /bin/ls。
int main() {
//pid_t pid = fork();
char * args[3];
args[0] = "type";
args[1] = "ls";
args[2] = NULL;
//if (!pid)
execvp(args[0], args);
return 0;
}
当我直接在 Ubuntu bash 上运行 type 时,它会打印出 ls is hashed (/bin/ls)。
区别在于type 是一个bash 内部命令,而ls 不是。但是为什么 Ubuntu 上的 bash 与 MacOS 上的行为不同呢?
Ubuntu bash 版本:GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
MacOS bash 版本:GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
纯粹根据版本号判断(这可能是不正确的做法),旧版本可以正确打印输出,而新版本则不能?
【问题讨论】:
-
你的
PATH是什么?在 MacOSX 上可能有所不同。或者那里的目录不一样。也许 MacOSX 有一些/usr/bin/type的东西,但 Ubuntu 没有任何这样的东西