How can I implement cd (change dir) command in Ruby?

S

Sam Kong

Hi,

This question is rather out of curiosity than practical.

Let's say I want to make a script which is equivalent to cd command in
Linux.
Is it possible?

Usage:

~$ ruby mycd.rb /temp
/temp$ <- current dir has been changed after the script ran.


system("cd #{ARGV[0]}") didn't work.

Thanks.

Sam
 
S

Sami Samhuri

Hi,

This question is rather out of curiosity than practical.

Let's say I want to make a script which is equivalent to cd command in
Linux.
Is it possible?

Only if your shell is done in Ruby as well. chdir(2) changes the
directory for the process which invokes it. cd is a shell built-in,
one which has no analog in /bin.
system("cd #{ARGV[0]}") didn't work.

That will spawn a new process running /bin/sh. That shell will change
its directory and then exit. afaik what you want is not possible
without resorting to (ugly) trickery.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top