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

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

    You can use . to execute a script in the current shell environment:

    . script_name
    

    or alternatively, its more readable but shell specific alias source:

    source script_name
    

    This avoids the subshell, and allows any variables or builtins (including cd) to affect the current shell instead.

提交回复
热议问题