Why I can't change directories using “cd”?

后端 未结 30 2944
眼角桃花
眼角桃花 2020-11-21 06:17

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 save

30条回答
  •  甜味超标
    2020-11-21 06:43

    You can create a function like below in your .bash_profile and it will work smoothly.

    The following function takes an optional parameter which is a project. For example, you can just run

    cdproj
    

    or

    cdproj project_name
    

    Here is the function definition.

    cdproj(){
        dir=/Users/yourname/projects
        if [ "$1" ]; then
          cd "${dir}/${1}"
        else
          cd "${dir}"
        fi
    }
    

    Dont forget to source your .bash_profile

提交回复
热议问题