Question

I tried to change directories by using backticks in Ruby. I am on Ubuntu 12.04 box and this is the error I get:

irb(main):003:0> `cd /`
(irb):3: command not found: cd /
=> ""

I run the command directly via the terminal and I change directories just fine. It also works fine on my local Mac box, so I'm not sure what is going on.

Was it helpful?

Solution

When you run shell commands using the tick marks, they will be run in a child process that can not change the working directory of the parent.

If you link the cd command with another command, you will see it work, but only effecting the child process:

`cd / && ls`

To change the working directory of the parent process use the Ruby command Dir.chdir([string]):

Dir.chdir("/")

Relevant information can be found in this post: "exec the cd command in a ruby script"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top