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 can combine an alias and a script,
provided that the script echos the destination path. Note that those are backticks surrounding the script name.
For example, your script could be
The advantage with this technique is that the script could take any number of command line parameters and emit different destinations calculated by possibly complex logic.
Using Bash Profile Functions :
One feature of the bash profile is to store custom functions that can be run in the terminal or in bash scripts the same way you run application/commands this also could be used as a shortcut for long commands.
To make your function efficient system widely you will need to copy your function at the end of several files
You can
sudo kwrite /home/user/.bashrc /home/user/.bash_profile /root/.bashrc /root/.bash_profile
to edit/create those files quicklyScript Example
Making shortcut to
cd ..
withcdd
ls shortcut
ls shortcut
Howto :
Copy your function at the end of your files and reboot your terminal you can then run
cdd
or whatever the function you wroteI got my code to work by using
. <your file name>
./<your file name>
dose not work because it doesn't change your directory in the terminal it just changes the directory specific to that script.Here is my program
Here is my terminal
On my particular case i needed too many times to change for the same directory. So on my .bashrc (I use ubuntu) i've added the
1 -
2-
3 -
Directly it will do: cd /home/tree/projects/java
Hope that helps!
You can combine Adam & Greg's alias and dot approaches to make something that can be more dynamic—
Now running the project alias will execute the project script in the current shell as opposed to the subshell.
Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The
cd
succeeds, but as soon as the subshell exits, you're back in the interactive shell and nothing ever changed there.One way to get around this is to use an alias instead: