How to run 'cd' in shell script and stay there after script finishes?

后端 未结 5 1068
温柔的废话
温柔的废话 2020-12-02 15:22

I used \'change directory\' in my shell script (bash)

#!/bin/bash
alias mycd=\'cd some_place\'
mycd
pwd

pwd prints some_

5条回答
  •  情歌与酒
    2020-12-02 15:50

    You need to source the file as:

    . myfile.sh
    

    or

    source myfile.sh
    

    Without sourcing the changes will happen in the sub-shell and not in the parent shell which is invoking the script. But when you source a file the lines in the file are executed as if they were typed at the command line.

提交回复
热议问题