How to (terminal) cd in folder from ruby script

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

    This isn't possible, to my knowledge, since they're different processes. If you need to affect the Ruby script's parent process (Bash), Bash will need to source something. So you could have your Ruby script output some bash commands, then have a Bash wrapper that source's Ruby's output.

    Try this as an example, in Bash:

    `ruby -e 'puts "cd /tmp"'` | source /dev/stdin
    

    But this will only work in Bash 4.0.

    EDIT:

    Actually, you can do this with eval instead of source in Bash 3:

    eval $(ruby -e'puts "cd /tmp"')
    

提交回复
热议问题