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

后端 未结 5 1071
温柔的废话
温柔的废话 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:57

    While sourcing the script you want to run is one solution, you should be aware that this script then can directly modify the environment of your current shell. Also it is not possible to pass arguments anymore.

    Another way to do, is to implement your script as a function in bash.

    function cdbm() {
        cd whereever_you_want_to_go
         echo arguments to the functions were $1, $2, ...
    }
    

    This technique is used by autojump: http://github.com/joelthelion/autojump/wiki to provide you with learning shell directory bookmarks.

提交回复
热议问题