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

后端 未结 30 2365
眼角桃花
眼角桃花 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:36

    While sourcing the script you want to run is one solution, you should be aware that this script then can directly modify the environment of your current shell. Also it is not possible to pass arguments anymore.

    Another way to do, is to implement your script as a function in bash.

    function cdbm() {
      cd whereever_you_want_to_go
      echo "Arguments to the functions were $1, $2, ..."
    }
    

    This technique is used by autojump: http://github.com/joelthelion/autojump/wiki to provide you with learning shell directory bookmarks.

提交回复
热议问题