The following code successfully lists the contents of current directory on Ubuntu bash and MacOS bash.
int main() {
char* args[3];
args[0] = "ls";
args[1] = NULL;
args[2] = NULL;
execvp(args[0], args);
return 0;
}
The following code doesn't print anything on Ubuntu bash but prints ls is /bin/ls
on MacOS bash.
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;
}
When I run type
on Ubuntu bash directly, it prints ls is hashed (/bin/ls)
.
The difference is that type
is a bash internal command while ls
is not. But why does bash on Ubuntu behave differently from that on MacOS?
Ubuntu bash version: GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
MacOS bash version: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Judging purely by version numbers (which may be an incorrect thing to do), older version prints output correctly while newer version doesn't?