简体   繁体   中英

Create an bash alias to link change directory in terminal

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. Eg 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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM