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?
This should do what you want. Change to the directory of interest (from within the script), and then spawn a new bash shell.
If you run this, it will take you to the directory of interest and when you exit it it will bring you back to the original place.
This will even take you to back to your original directory when you exit (CTRL+d)
You can execute some lines in the same subshell if you end lines with backslash.
You're doing nothing wrong! You've changed the directory, but only within the subshell that runs the script.
You can run the script in your current process with the "dot" command:
But I'd prefer Greg's suggestion to use an alias in this simple case.
to navigate directories quicky, there's $CDPATH, cdargs, and ways to generate aliases automatically
http://jackndempsey.blogspot.com/2008/07/cdargs.html
http://muness.blogspot.com/2008/06/lazy-bash-cd-aliaes.html
https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-5827311.html
In your ~/.bash_profile file. add the next function
Restart terminal and you can type
and you will be moved to the destination folder.
The
cd
is done within the script's shell. When the script ends, that shell exits, and then you are left in the directory you were. "Source" the script, don't run it. Instead of:do
(Notice the dot and space before the script name.)