I am trying to write a program that will change the current directory in zsh without creating a new instance of zsh. Might sound confusing but what I've tried so far should help with that.
Say zsh is currently in the "Users" directory as in:
$ /Users/ →
If I run the following on Python:
os.system("cd projects")
os.system("zsh")
It changes zsh to the "Projects" directory:
$ /Users/ → python main.py
$ /Users/Projects/ →
But this creates a new instance of zsh and running exit doesn't close the Terminal window. Instead, I'll need to rerun exit.
Is there anyway to get Python to change current directory in zsh without starting up another instance of it?
zsh, true, but the Python script is also still running, blocking until the newzshexits, at which pointos.systemreturns and your Python script can continue. Also, I cannot reproduce the working directory having been changed by the code as show. The firstos.systemstarts a new shell, executes thecdcommand, then that shell exits. The shell started byzshwill still have whatever working directory Python has.