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

Discussion in 'Ruby' started by Sam Kong, Jun 21, 2007.

  1. Sam Kong

    Sam Kong Guest

    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
     
    Sam Kong, Jun 21, 2007
    #1
    1. Advertising

  2. Sam Kong

    Sami Samhuri Guest

    On 6/21/07, Sam Kong <> wrote:
    > 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.

    --
    Sami Samhuri
     
    Sami Samhuri, Jun 21, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UnVkeQ==?=

    Sub Dir, Virtual dir, what do I use?

    =?Utf-8?B?UnVkeQ==?=, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    448
    =?Utf-8?B?UnVkeQ==?=
    Jun 12, 2005
  2. Peter Vestergaard

    command prompt change dir

    Peter Vestergaard, Jul 11, 2003, in forum: Python
    Replies:
    3
    Views:
    423
    Peter Vestergaard
    Jul 14, 2003
  3. Matthew Denner
    Replies:
    1
    Views:
    217
  4. Kga Agk
    Replies:
    2
    Views:
    168
    Kga Agk
    Jun 29, 2009
  5. Nick Gnedin
    Replies:
    2
    Views:
    184
Loading...

Share This Page