I'm trying to write a small script to change the current directory to my project directory:
#!/bin/bash
cd /home/tree/projects/java
I saved this file as proj, added execute permission with chmod
, and copied it to /usr/bin
. When I call it by:
proj
, it does nothing. What am I doing wrong?
LOOOOOng time after, but I did the following:
create a file called case
paste the following in the file:
save it and then:
I also created an alias in my
.bashrc
:now when I type:
essentially I am typing:
You can type any folder after 'case':
which is like typing:
respectively
In my case the path is much longer - these guys summed it up with the ~ info earlier.
While sourcing the script you want to run is one solution, you should be aware that this script then can directly modify the environment of your current shell. Also it is not possible to pass arguments anymore.
Another way to do, is to implement your script as a function in bash.
This technique is used by autojump: http://github.com/joelthelion/autojump/wiki to provide you with learning shell directory bookmarks.
simply run:
It only changes the directory for the script itself, while your current directory stays the same.
You might want to use a symbolic link instead. It allows you to make a "shortcut" to a file or directory, so you'd only have to type something like
cd my-project
.You can create a function like below in your
.bash_profile
and it will work smoothly.The following function takes an optional parameter which is a project. For example, you can just run
or
Here is the function definition.
Dont forget to source your
.bash_profile
You can do following:
EDIT: This could be 'dotted' as well, to prevent creation of subsequent shells.
Example: