Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a scenario where I need to cd into a directory before running a script:

cd repos; python script.py

But it should be setup such that it automatically runs when screen is started. So in .screenrc I have this:

screen -t "cd repos; python script.py"

except this doesn't work because the cd repos is wrong. How do I cd into this directory?

share|improve this question
Why don't you use absolute path to "repos" dir? – gelraen Sep 27 '11 at 13:52
up vote 2 down vote accepted

If you want this to happen by default when you run screen, then you'll need to edit your .screenrc file. You can use the chdir directive to have a new screen window change directories upon opening it, but you'll want to explicitly reset that after your needs are met, so that newer windows are not affected.

# .screenrc settings to cd to repos and run python-script.py
chdir /full/path/to/repos
screen -t "Python Script" ./python-script.py

# reset the default directory for new windows.
chdir $HOME
screen -t "New Window" bash
screen -t "SSH Session" ssh foo@bar.com

Keep in mind that this will essentially set "./python-script.py" as the shell for that window, so when python-script.py is finished the window will close.

share|improve this answer
Thanks, that works! – MrD Sep 28 '11 at 6:53

The option -t is for the title, so is wrong in your context.

I would write

screen sh -c 'cd repos; python script.py'

Some observations:

  1. a correctly setup application should not depend on the working directory to work properly, so screen python repos/script.py should work as well (not taking into account that the same explicit invokation of python could be avoided)

  2. if you put that into ~/.screenrc, the command will be executed on each new screen invokation. Are you sure you want that?

share|improve this answer
I agree 100% with observation #1. Fix that and this question would have never come up. – Tim Kennedy Sep 27 '11 at 14:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.