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

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

    Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The cd succeeds, but as soon as the subshell exits, you're back in the interactive shell and nothing ever changed there.

    One way to get around this is to use an alias instead:

    alias proj="cd /home/tree/projects/java"
    

提交回复
热议问题