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

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

    The cd in your script technically worked as it changed the directory of the shell that ran the script, but that was a separate process forked from your interactive shell.

    A Posix-compatible way to solve this problem is to define a shell procedure rather than a shell-invoked command script.

    jhome () {
      cd /home/tree/projects/java
    }
    

    You can just type this in or put it in one of the various shell startup files.

提交回复
热议问题