开发者

Automatic changes to $PATH in Bash

The project I work on has some executable scripts in the repository. These scripts are actually tools that automate some development tasks and I invoke them only when I'm inside the repo and they work only on files in the repo. My typical working session looks like this:

$ cd $REPO
$ ./tools/start-session
$ some-script some arguments
$ other-script other arguments

In other words, the start-session script just adds an appropriate entry to the $PATH variable.

I'd like to automa开发者_JAVA百科te it further so that I don't have to call the $REPO/tools/start-session script. What I'd like to achieve is to make bash automatically detect that I'm in my repo dir and append proper entry to $PATH. The point is that I have many working copies of my repo and the tools differ a bit in each branch, I move between working copies frequently and I'd like my shell to kind of guess which script I want to invoke.

How do I do that?

The only thing I can think of is that the $PS1 variable gets executed each time a command is completed, so I could somehow hook some $PATH-changing script in there using backticks, i.e. do some thing like

PS1=`update-path`$PS1

But this seems not the right way.


On the right track, but don't use PS1 - you want PROMPT_COMMAND. For example:

 PROMPT_COMMAND='ls'

will execute ls every time a new prompt appears. Whether this will solve you root problem I couldn't say, as I'm not sure I understand it properly.


I would create a simple bash script - let's call it cs (change session) - which changes directory and updates path, so instead of

$ cd $REPO
$ ./tools/start-session
$ some-script some arguments
$ other-script other arguments

you will do

$ cs $REPO
$ some-script some arguments
$ other-script other arguments

Something like this:

#!/bin/bash
cd $1
./tools/start-session
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜