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

后端 未结 30 2419
眼角桃花
眼角桃花 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 06:28

    The cd is done within the script's shell. When the script ends, that shell exits, and then you are left in the directory you were. "Source" the script, don't run it. Instead of:

    ./myscript.sh
    

    do

    . ./myscript.sh
    

    (Notice the dot and space before the script name.)

提交回复
热议问题