0

尝试使用此处找到的答案:

如何在 shell 脚本中运行“cd”并在脚本完成后留在那里?

当我添加'source'命令时,脚本运行后目录仍然不变,无论我是执行'source'还是使用cshrc中编码的别名调用脚本。

任何帮助深表感谢!

4

2 回答 2

1

正如您在下面看到的,确保您的调用cd没有在子shell 中执行如果是,这将不起作用,source或者不是。

cd带有子shell的脚本

#!/bin/bash

( cd /etc ) # thie exec's in a subshell

输出

$ pwd
/home/siegex
$ source ./cdafterend.sh && pwd
/home/siegex

cd 不在子shell中的脚本

#!/bin/bash

cd /etc # no subshell here

输出

$ pwd
/home/siegex
$ source ./cdafterend.sh && pwd
/etc
于 2010-12-17T19:59:43.353 回答
0

必须从该脚本中的 cd 命令中删除“/bin/”,以便该命令按预期工作。删除它会删除此脚本的子外壳问题。此外,在这种情况下,ls 命令中的编码“$1”是无效的。

于 2011-01-12T16:36:07.733 回答