Bash 为什么更改目录(cd)在这个mac脚本中不起作用?
我将此脚本保存在名为/Users/tcl/scripts/gotoroot的文件中: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$ 目录没有改变,我不知道为什么?
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正在工作,但它发生在与主终端会话不同的进程中,并且在返回终端会话后对工作目录没有影响 明白了,谢谢。这是一个复制品。