简体   繁体   English

在系统调用和命令提示符中调用“ cd”之间的区别

[英]difference between calling 'cd' in system call and command prompt

I am using a linux command prompt and I have noticed a difference when using the command 'cd' in the command prompt and using the command as an argument of the system call in a C program. 我正在使用linux命令提示符,并且在命令提示符中使用命令“ cd”并将命令用作C程序中系统调用的参数时,我已经注意到了差异。

When I use the command prompt, the directory changes to the directory in the $home environment variable, but if I call it using the system call, then the directory doesn't change? 当我使用命令提示符时,目录会更改为$ home环境变量中的目录,但是如果我使用系统调用来调用,那么目录不会更改吗?

Why is this happening? 为什么会这样呢?

The cd command is a shell builtin -- when it is executed by a shell script or on the command prompt, it is interpreted by that shell directly, and changes the working directory of the shell process. cd命令是Shell内置的-当它由Shell脚本执行或在命令提示符下执行时,它将直接由该Shell解释,并更改Shell进程的工作目录。

When you run a cd command from within a system() call, the command is passed to a shell subprocess, and changes the working directory of that process, which promptly exits. 当您从system()调用中运行cd命令时,该命令将传递到Shell子进程,并更改该进程的工作目录,该目录会立即退出。 The working directory of the parent process is left unchanged. 父进程的工作目录保持不变。

If you need to change the working directory of the current process, call chdir() directly. 如果需要更改当前进程的工作目录,请直接调用chdir() Note that you can't change the working directory of other processes (the child process is independent of its parent). 请注意,您不能更改其他进程的工作目录(子进程独立于其父进程)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM