How to (terminal) cd in folder from ruby script

后端 未结 4 993
逝去的感伤
逝去的感伤 2021-01-05 01:53

I\'d like to know if it\'s possible to change the current terminal directory from where I\'m executing my ruby script.

For example, if I\'m executing the script from

4条回答
  •  粉色の甜心
    2021-01-05 02:12

    Maybe slightly easier,

    You can use exec in combination with Maerics post about creating a new bash shell inside of ruby to achieve this.

    exec ruby -e "Dir.chdir( 'mydir' ); exec 'bash'"
    

    exec will replace the parent process(shell) with the child process (ruby), thus the shell no longer exists (no need to exit).

    At the end of the ruby routine bash is fired up (again with exec this time in ruby) in the new directory and replaces the ruby process.

提交回复
热议问题