0

I am trying to learn the basics of shell. I used vim editro for creating my own list of commands to be executed. Here is the way I created the code vi mycommands

then inside this file I wrote

   cd Documents

I am using macOS Catalina which has zsh by default but switched to bash

So when I write the following command in the terminal:

   $ sh +x mycommands

It shows

+cd Documents

The Documents has some files and directories but it is not changing directory.Where am I going wrong? Any help will be greatly appreciated.

1 Answers1

2

Scripts run like sh myscript execute in a separate sub-shell, not the current shell. Changing directory inside a script will not cause your shell to change directory. If you want to change directory in your shell, you need to run the commands in your shell.

To do that, run:

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

See this question.

chash
  • 3,975
  • 13
  • 29