Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 为什么更改目录(cd)在这个mac脚本中不起作用?_Bash - Fatal编程技术网

Bash 为什么更改目录(cd)在这个mac脚本中不起作用?

Bash 为什么更改目录(cd)在这个mac脚本中不起作用?,bash,Bash,我将此脚本保存在名为/Users/tcl/scripts/gotoroot的文件中: echo "hello" cd / echo "good bye" 但当我运行它时,我得到了: User:scripts tcl$ pwd /Users/tcl/scripts User:scripts tcl$ gotoroot hello good bye User:scripts tcl$ pwd /Users/tcl/scripts User:scripts tcl$ 目录没有改变,我不知道为什么?

我将此脚本保存在名为/Users/tcl/scripts/gotoroot的文件中:

echo "hello"
cd /
echo "good bye"
但当我运行它时,我得到了:

User:scripts tcl$ pwd
/Users/tcl/scripts
User:scripts tcl$ gotoroot
hello
good bye
User:scripts tcl$ pwd
/Users/tcl/scripts
User:scripts tcl$

目录没有改变,我不知道为什么?它应该是/,而不是/Users/tcl/scripts

运行脚本时,它会在新进程中启动子shell。
cd
更改子shell中的目录,而不是终端进程中的目录

通过在脚本中添加以下内容进行测试:

pwd
cd /
pwd

您应该看到它在脚本中变为
/

Shell脚本在子进程中运行。CD正在工作,但它发生在与主终端会话不同的进程中,并且在返回终端会话后对工作目录没有影响

明白了,谢谢。这是一个复制品。