How to (terminal) cd in folder from ruby script

后端 未结 4 988
逝去的感伤
逝去的感伤 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:15

    I doubt there is a straightforward way to do this.

    When you launch a program from your login shell, underneath the hood it's doing a fork/exec which means a new (child) process is created (which has its own environment, such as current working directory) and it's standard input/output/error streams are associated with the (parent) shell process. So when your script calls Dir.chdir it changes the current working directory for the script (child) process, not the parent.

    Perhaps your script could launch a new shell before it exits, so it looks like your terminal has changed directory. Although, you'd have to exit from that extra shell before exiting your terminal session...

提交回复
热议问题