# Shell \<-\> Ruby question

**URL:** https://rubytalk.org/t/shell-ruby-question/2506
**Category:** ruby-talk
**Created:** [27 October 2002 02:27 UTC](https://rubytalk.org/t/shell-ruby-question/2506 "2002-10-27T02:27:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Christian\_Szegedy1](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Christian\_Szegedy1](https://rubytalk.org/u/Christian_Szegedy1)
#### Post date: [27 October 2002 02:27 UTC](https://rubytalk.org/t/shell-ruby-question/2506/1 "2002-10-27T02:27:38Z")

</div>

This is not a strictly Ruby question, but perhaps  
you have some idea which could help me.

Recently I have written some some concise ruby scripts  
which help me while working in the (bash) shell. (E.g.  
x extracts any archive etc.)

I also wanted to write a more intelligent cd  
(change sirectory) script in Ruby. Of course there is  
an obvious problem I can’t solve: the bash opens  
a new shell for the script, so the working directory  
remains unchanged in the shell.

I also tried to run ruby with

… /usr/bin/ruby

I thought, it would run ruby in the current environment  
(like ksh), but it simpy gives the error:

bash: ELF: command not found

Does someone have an idea, how could I change the  
working directory by a Ruby script?

Regards, Christian

---

<div class="post-metadata">

### Author: ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)
#### Post date: [27 October 2002 02:46 UTC](https://rubytalk.org/t/shell-ruby-question/2506/2 "2002-10-27T02:46:02Z")

</div>

Hi,

> Does someone have an idea, how could I change the  
> working directory by a Ruby script?

Fundamently, child processes can never modify directly parent  
process’es environment. Unix process model prohibits it. You  
can just receive child’s output and cd by the shell process  
itself.

foo() {eval `ruby somescript`}

# somescript

puts “cd #{directroy}”

> **···**
>
> At Sun, 27 Oct 2002 11:27:38 +0900, Christian Szegedy wrote:
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

### Author: ![YANAGAWA\_Kazuhisa](https://avatars.discourse-cdn.com/v4/letter/y/d78d45/32.png) [@YANAGAWA\_Kazuhisa](https://rubytalk.org/u/YANAGAWA_Kazuhisa)
#### Post date: [27 October 2002 03:38 UTC](https://rubytalk.org/t/shell-ruby-question/2506/3 "2002-10-27T03:38:36Z")

</div>

> I also wanted to write a more intelligent cd  
> (change sirectory) script in Ruby. Of course there is  
> an obvious problem I can’t solve: the bash opens  
> a new shell for the script, so the working directory  
> remains unchanged in the shell.

How about ruby script only make a path to cd and do actual cd by shell  
function or alias? Namely:

```
~> function mcd () {cd `ruby /tmp/foo.rb`}
~> cat > /tmp/foo.rb
puts "/tmp"
^D
~> mcd
/tmp>

```

Of course a script may do more useful thing, and a function/alias must  
be more safe; at least a function/alias should check exit status of  
the script and the script signals failure on such as no directory to  
cd being found.

> . /usr/bin/ruby
> 
> I thought, it would run ruby in the current environment

No, probably ksh also doesn’t. `.' in sh-derived shells means `run  
the _script_ in the current context. So `. /usr/bin/ruby’ evaluates  
ruby itself as shell script.

> **···**
>
> In message [apfia9$aq1$05$1@news.t-online.com](mailto:apfia9$aq1$05$1@news.t-online.com) [szegedy@t-online.de](mailto:szegedy@t-online.de) writes:
> 
> –  
> kjana@dm4lab.to October 27, 2002  
> Behind the clouds is the sun still shining.

---

<div class="post-metadata">

### Author: ![Christian\_Szegedy1](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Christian\_Szegedy1](https://rubytalk.org/u/Christian_Szegedy1)
#### Post date: [27 October 2002 13:28 UTC](https://rubytalk.org/t/shell-ruby-question/2506/4 "2002-10-27T13:28:11Z")

</div>

YANAGAWA Kazuhisa wrote:

How about ruby script only make a path to cd and do actual cd by shell  
function or alias? Namely:

```
 ~> function mcd () {cd `ruby /tmp/foo.rb`}
 ~> cat > /tmp/foo.rb

```

Thanks! This was a great idea! It solves my problem.

Thank you for the help!

Regards, Christian
