I'm writing a simple script which ideally will help me conveniently change directories around my system.
The details of the implementation don't matter, but let's say ideally I will place this script in /usr/bin and call it with an argument denoting where I want to go to on the system: goto project1
I would expect that when the script exits, my terminal's current working would have changed to that of Project 1.
In order to accomplish this, I tried:
os.chdir('/') subprocess.call('cd /', shell=True) Neither of which work. The first changes the working directory in Python and the second spawns a shell at /.
Then I realized how naive I was being. When a program is run, the terminal is just forking a process, while reading from stdout, which my program is writing to. Whatever it does, it wouldn't affect the state of terminal.
But then I thought "I've been using cd for years, surely someone wrote code for that", thinking there might be something to go off of (system call or something?).
But cd is not even coreutils. Instead, the source of cd is this:
builtin `echo ${0##*/} | tr \[:upper:] \[:lower:]` ${1+"$@"} So, a couple of questions come to mind:
- What's actually going on behind the scenes when a user calls
cd? (Meaning, how is the terminal and the system actually interacting?) - Is it possible to have something like a Python script alter the terminal location?
Thanks so much for your help!
bashallow this though? If I were to run a script written inbashto do this, I would (obviously) be able to change it.echo 'cd /' > cd.sh; ./cd.sh, and you'll find that your working directory is unchanged.