1

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!

5
  • "Change caller's current working directory" - as far as I'm aware, that's simply not possible. You can't screw with another process's working directory, even if they asked you to. Commented May 13, 2016 at 5:33
  • I figured this might have been the answer. Why does bash allow this though? If I were to run a script written in bash to do this, I would (obviously) be able to change it. Commented May 13, 2016 at 5:37
  • Nope! Try it. echo 'cd /' > cd.sh; ./cd.sh, and you'll find that your working directory is unchanged. Commented May 13, 2016 at 5:38
  • Hmmm, yeah you're right. What really inspired all of this was this script which changes directories and I didn't know if something was possible in Python. I don't really get how either work. Commented May 13, 2016 at 5:43
  • I'm not a bash expert, and I can't test that script at the moment, but it doesn't look to me like it changes the caller's directory. It looks to me like it just defines a function that changes directories. Commented May 13, 2016 at 5:56

1 Answer 1

2

You could do it as a pair of scripts:

directory.py

#!/usr/bin/python import sys directory = sys.argv[1] # do something interesting to manipulate directory... print directory + "tmp" 

directory.csh

#!/bin/csh -f cd `python directory.py $1` 

Result:

> pwd /Users/cdl > source directory.csh "/" > pwd /tmp > 

Substitute your favorite shell and variant of Python as desired. Turn on execute for the Python script to simplify further.

Clearly the shell is changing the directory but Python can do all the clever logic you want to figure out where to send the shell.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.