# Declare a shell alias in a ruby file

**URL:** <https://www.ruby-forum.com/t/declare-a-shell-alias-in-a-ruby-file/174898>\
**Category:** Ruby\
**Created:** [September 3, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/declare-a-shell-alias-in-a-ruby-file/174898 "2009-09-03T17:25:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 3, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/declare-a-shell-alias-in-a-ruby-file/174898/1 "2009-09-03T17:25:44Z")

</div>

Hello,

I’m trying to declare a shell alias from a ruby file.

here is the file test.rb :

# begin test.rb

`alias world='echo hello'`

# end test.rb

I’m launching it :

> ruby test.rb

Then launching the alias :

> world  
> -bash: world: command not found

Does anyone know how do i declare a shell alias from a ruby file ?

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 3, 2009, 6:16pm UTC](https://www.ruby-forum.com/t/declare-a-shell-alias-in-a-ruby-file/174898/2 "2009-09-03T18:16:06Z")

</div>

At 2009-09-03 11:22AM, “jney” wrote:

> Hello,
> 
> I’m trying to declare a shell alias from a ruby file.
> 
> here is the file test.rb :
> 
> # begin test.rb
> 
> `alias world='echo hello'`

you spawn a shell, declare the alias, then the shell exits taking the  
alias with it.

> I’m launching it :
> 
> > ruby test.rb
> 
> Then launching the alias :
> 
> > world  
> > -bash: world: command not found
> 
> Does anyone know how do i declare a shell alias from a ruby file ?

If you want a child process to make changes to the current process, you  
have to make the child tell the parent what to do and then the parent  
has to do it.

#### alias.rb outputs shell commands

puts \<\<END  
alias world=‘echo hello’  
END

#### shell evaluates the output of the ruby program

eval $(ruby alias.rb)  
world

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 3, 2009, 6:16pm UTC](https://www.ruby-forum.com/t/declare-a-shell-alias-in-a-ruby-file/174898/3 "2009-09-03T18:16:09Z")

</div>

Jean-sÃ©bastien Jney wrote:

> Hello,
> 
> I’m trying to declare a shell alias from a ruby file.
> 
> here is the file test.rb :
> 
> # begin test.rb
> 
> `alias world='echo hello'`
> 
> # end test.rb
> 
> I’m launching it :
> 
> > ruby test.rb
> 
> Then launching the alias :
> 
> > world  
> > -bash: world: command not found
> 
> Does anyone know how do i declare a shell alias from a ruby file ?

You cannot.

The processes are started as follows:

your shell --------------\> ruby process --------\> another shell  
ruby test.rb `xxx`

Each process is completely independent of the others - with its own  
address space, and its own copy of the ENVironment. When the second  
shell terminates, it cannot affect the ruby process, nor the original  
shell which started that ruby process.

The only way you can do this is from the original shell itself. Either  
type your alias command at the shell prompt, or put it in a file and do

. myscript

The dot (.) means “read this file and execute it as commands within this  
shell”

You may find the comp.unix.shell FAQ helpful.

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 4, 2009, 12:30pm UTC](https://www.ruby-forum.com/t/declare-a-shell-alias-in-a-ruby-file/174898/4 "2009-09-04T12:30:55Z")

</div>

At 2009-09-04 04:32AM, “Jean-Sébastien” wrote:

> ruby one, althrough my file is starting by “#!/usr/bin/env ruby”.  
> Brian is saying that the ruby script writes shell commands to a file  
> named “myscript” (instead of printing them to stdout). Then the shell  
> will source that file (instead of ‘eval’-ing its stdin).

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 4, 2009, 10:43am UTC](https://www.ruby-forum.com/t/declare-a-shell-alias-in-a-ruby-file/174898/5 "2009-09-04T10:43:11Z")

</div>

On 3 sep, 18:16, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I’m launching it :  
> > The processes are started as follows:  
> > type your alias command at the shell prompt, or put it in a file and do
> 
> . myscript
> 
> The dot (.) means “read this file and execute it as commands within this  
> shell”
> 
> ## You may find the comp.unix.shell FAQ helpful.
> 
> Posted viahttp://www.ruby-forum.com/.

Thank you both of you.

eval $(ruby test.rb) works for me.  
But output have to a one line command separate by “;”.

“. myscript” doesn’t not work for me. It doesn’t read my file as a  
ruby one, althrough my file is starting by “#!/usr/bin/env ruby”.

Thanks.

---

<div class="post-metadata">

**Author:** ![jney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@jney](https://www.ruby-forum.com/u/jney)\
**Post date:** [September 5, 2009, 12:02am UTC](https://www.ruby-forum.com/t/declare-a-shell-alias-in-a-ruby-file/174898/6 "2009-09-05T00:02:58Z")

</div>

Glenn,

Do you know how to unsubscribe from these e-mails?

Thanks,  
Steve

— On Fri, 9/4/09, Glenn J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

From: Glenn J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: declare a shell alias in a ruby file  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Friday, September 4, 2009, 6:30 AM

At 2009-09-04 04:32AM, “Jean-SÃ©bastien” wrote:

> Â ruby one, althrough my file is starting by “#!/usr/bin/env ruby”.  
> Brian is saying that the ruby script writes shell commands to a file  
> named “myscript” (instead of printing them to stdout).Â Then the shell  
> will source that file (instead of ‘eval’-ing its stdin).
