执行用户定义 shell 命令时出现问题

Problem in executing user defining shell commmand

我正在尝试学习 shell 的基础知识。我使用 vim editro 创建了我自己的要执行的命令列表。这是我创建代码的方式 vi mycommands

然后我在这个文件里面写了

   cd Documents

我使用的是 macOS Catalina,它默认有 zsh 但切换到了 bash

所以当我在终端中写入以下命令时:

   $ sh +x mycommands

显示

+cd Documents

Documents 有一些文件和目录,但没有改变 directory.Where 我是不是搞错了? 任何帮助将不胜感激。

sh myscript 这样的脚本 运行 在单独的子 shell 中执行,而不是当前的 shell。在脚本中更改目录不会导致您的 shell 更改目录。如果您想更改 shell 中的目录,您需要 运行 shell 中的命令。

为此,运行:

. ./myscript (sh, bash) 或 source ./myscript (bash).

看到这个question