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?
You need no script, only set the correct option and create an environment variable.
in your
~/.bashrc
allows tocd
to the content of environment variables.Create such an environment variable:
and you can use:
Other alternatives.
You can use the operator && :
cd myDirectory && ls
To make a bash script that will cd to a select directory :
Create the script file
Then create an alias in your startup file.
For example, create a master aliases/functions file: /scripts/mastercode.sh
(Put the alias in this file.)
Then at the end of your .bashrc file:
Now its easy to cd to your java directory, just type cdjava and you are there.
Jeremy Ruten's idea of using a symlink triggered a thought that hasn't crossed any other answer. Use:
The leading colon is important; it means that if there is a directory 'dir' in the current directory, then '
cd dir
' will change to that, rather than hopping off somewhere else. With the value set as shown, you can do:and, if there is no sub-directory called java in the current directory, then it will take you directly to $HOME/projects/java - no aliases, no scripts, no dubious execs or dot commands.
My $HOME is /Users/jleffler; my $CDPATH is:
You can use
.
to execute a script in the current shell environment:or alternatively, its more readable but shell specific alias
source
:This avoids the subshell, and allows any variables or builtins (including
cd
) to affect the current shell instead.