Reputation: 496
I have written the following code (and added it to my .bash_aliases file in Ubuntu) to change directory in my terminal to the project I enter. E.g. if I enter go project1
it will search through my freelance and side_projects directory and if it has the "project1" directory it will cd into that directory.
alias go='goToProject'
function goToProject
{
echo 'Redirecting to' $1
if [ -d "side_projects/$1" ]; then
cd ~/Documents/projects/personal/side_projects/$1
fi
if [ -d "freelance/$1" ]; then
cd ~/Documents/projects/personal/freelance/$1
fi
}
However when I run this code it prints the "Redirecting to project1" but doesn't change directory. Can anyone see an obvious error in my code?
Upvotes: 1
Views: 223
Reputation: 88626
Or add this to your .bashrc:
CDPATH="$CDPATH:$HOME/Documents/projects/personal/side_projects:$HOME/Documents/projects/personal/freelance"
and you can use cd project1
from everywhere.
Upvotes: 4
Reputation: 157992
You don't need the alias, just this:
function go
{
echo "Redirecting to $1"
if [ -d "side_projects/$1" ]; then
cd ~/Documents/projects/personal/side_projects/"$1"
fi
if [ -d "freelance/$1" ]; then
cd ~/Documents/projects/personal/freelance/"$1"
fi
}
Btw, note that I've added missing quotes.
Upvotes: 3