Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run bash script on `cd` command

Tags:

python

linux

ruby

I'm python developer and most frequently I use buildout for managing my projects. In this case I dont ever need to run any command to activate my dependencies environment.

However, sometime I use virtualenv when buildout is to complicated for this particular case.

Recently I started playing with ruby. And noticed very useful feature. Enviourement is changing automatically when I cd in to the project folder. It is somehow related to rvm nad .rvmrc file.

I'm just wondering if there are ways to hook some script on different bash commands. So than I can workon environment_name automatically when cd into to project folder.

So the logic as simple as:

When you cd in the project with folder_name, than script should run workon folder_name

like image 710
Pol Avatar asked Jul 12 '13 17:07

Pol


People also ask

Can you use cd in bash script?

Trying to use cd inside the shell script does not work because the shell script runs in the subshell and once the script is over it returns to the parent shell, which is why the current directory does not change.

How do I run a bash script in CMD?

BASH will be available in the Command Prompt and PowerShell. Open Command Prompt and navigate to the folder where the script file is available. Type Bash script-filename.sh and hit the enter key. It will execute the script, and depending on the file, you should see an output.

How do I change directory in bash script?

To change directories, use the command cd followed by the name of the directory (e.g. cd downloads ). Then, you can print your current working directory again to check the new path.

What is cd in shell script?

The cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.


2 Answers

One feature of Unix shells is that they let you create shell functions, which are much like functions in other languages; they are essentially named groups of commands. For example, you can write a function named mycd that first runs cd, and then runs other commands:

function mycd () {
    cd "$@"
    if ... ; then
        workon environment
    fi
}

(The "$@" expands to the arguments that you passed to mycd; so mycd /path/to/dir will call cd /path/to/dir.)

As a special case, a shell function actually supersedes a like-named builtin command; so if you name your function cd, it will be run instead of the cd builtin whenever you run cd. In that case, in order for the function to call the builtin cd to perform the actual directory-change (instead of calling itself, causing infinite recursion), it can use Bash's builtin builtin to call a specified builtin command. So:

function cd () {
    builtin cd "$@"    # perform the actual cd
    if ... ; then
        workon environment
    fi
}

(Note: I don't know what your logic is for recognizing a project directory, so I left that as ... for you to fill in. If you describe your logic in a comment, I'll edit accordingly.)

like image 125
ruakh Avatar answered Oct 24 '22 00:10

ruakh


I think you're looking for one of two things.

autoenv is a relatively simple tool that creates the relevant bash functions for you. It's essentially doing what ruakh suggested, but you can use it without having to know how the shell works.

virtualenvwrapper is full of tools that make it easier to build smarter versions of the bash functions—e.g., switch to the venv even if you cd into one of its subdirectories instead of the base, or track venvs stored in git or hg, or … See the Tips and Tricks page.

The Cookbook for autoenv, shows some nifty ways ways to use the two together.

like image 36
abarnert Avatar answered Oct 24 '22 01:10

abarnert