# How do i open a terminal using ruby?

**URL:** <https://www.ruby-forum.com/t/how-do-i-open-a-terminal-using-ruby/143709>\
**Category:** Ruby\
**Created:** [July 22, 2008, 2:13am UTC](https://www.ruby-forum.com/t/how-do-i-open-a-terminal-using-ruby/143709 "2008-07-22T02:13:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [July 22, 2008, 2:13am UTC](https://www.ruby-forum.com/t/how-do-i-open-a-terminal-using-ruby/143709/1 "2008-07-22T02:13:54Z")

</div>

hi-

i want to open up a terminal from ruby if it was not executed in one.  
so,

if not STDIN.isatty  
#Open up a terminal  
end

do i have to use IO.popen? i would rather not because, i’m using linux  
but this ruby script will also be executed on windows. how do i account  
for when to use “gnome-terminal” and when to use “cmd”? then, how do i  
write to that terminal window?

is there something easier?

thanks!!!

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [July 22, 2008, 2:27am UTC](https://www.ruby-forum.com/t/how-do-i-open-a-terminal-using-ruby/143709/2 "2008-07-22T02:27:42Z")

</div>

On Jul 21, 2008, at 6:08 PM, Philip Rutkowski wrote:

> but this ruby script will also be executed on windows. how do i  
> account  
> for when to use “gnome-terminal” and when to use “cmd”? then, how do i  
> write to that terminal window?

I have no idea if this will work, but it’s worth a shot …

Kernel.exec ‘/bin/bash’, ‘-c’, “ruby #{ **FILE** } #{ARGV.join(’ ')}”

Beware the infinite recursion!

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [July 22, 2008, 5:38am UTC](https://www.ruby-forum.com/t/how-do-i-open-a-terminal-using-ruby/143709/3 "2008-07-22T05:38:59Z")

</div>

On Jul 21, 2008, at 6:08 PM, Philip Rutkowski wrote:

> but this ruby script will also be executed on windows. how do i  
> account  
> for when to use “gnome-terminal” and when to use “cmd”? then, how do i  
> write to that terminal window?
> 
> is there something easier?  
> thanks!!!

why?

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [July 22, 2008, 11:05am UTC](https://www.ruby-forum.com/t/how-do-i-open-a-terminal-using-ruby/143709/4 "2008-07-22T11:05:00Z")

</div>

Philip Rutkowski wrote:

> if not STDIN.isatty  
> Â #Open up a terminal  
> end  
> […]  
> is there something easier?

If I’m not mistaken, on Windows ruby scripts automatically open in a  
command  
window. And most filemanagers on unix ask you whether to open a terminal  
when  
clicking on an executable file. So I don’t think there’s any need to do  
what  
you’re trying to do.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [July 22, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/how-do-i-open-a-terminal-using-ruby/143709/5 "2008-07-22T15:42:36Z")

</div>

Sebastian H. wrote:

> Philip Rutkowski wrote:
> 
> > if not STDIN.isatty  
> > Â #Open up a terminal  
> > end  
> > […]  
> > is there something easier?
> 
> If I’m not mistaken, on Windows ruby scripts automatically open in a  
> command  
> window. And most filemanagers on unix ask you whether to open a terminal  
> when  
> clicking on an executable file. So I don’t think there’s any need to do  
> what  
> you’re trying to do.
> 
> HTH,  
> Sebastian

I have the same need. I have a script that runs in a terminal right  
now, but I eventually want to make it gui (probably with Shoes) and I’ll  
need an option to click a button and open a command prompt (then run a  
command in that command prompt and leave the prompt open for the user to  
use).

I haven’t even begun converting to the gui part yet though, so I haven’t  
put much thought into how I’m going to accomplish this. Oh yeah, and  
this will need to be cross-platform, too, but it may just be a matter of  
writing a slightly different program for each platform.

James

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [July 22, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/how-do-i-open-a-terminal-using-ruby/143709/6 "2008-07-22T18:54:13Z")

</div>

On Jul 22, 2008, at 6:37 AM, James D. wrote:

> put much thought into how I’m going to accomplish this. Oh yeah, and  
> this will need to be cross-platform, too, but it may just be a  
> matter of  
> writing a slightly different program for each platform.
> 
> ## James
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Check out Monkeybars ([http://monkeybars.rubyforge.org/](http://monkeybars.rubyforge.org/)) for GUI stuff.  
Combine that with Rawr and you should have a slick cross-platform GUI  
along with native .app/.exe files and/or a web start link. Monkeybars  
sits on JRuby, so you might be able to find some Java libraries to  
help you out along with your gems. A quick search for Java terminal  
stuff yielded JLine ([http://jline.sourceforge.net/](http://jline.sourceforge.net/)). Of course, that’s  
if the gem you had in mind isn’t adequate for what you’re doing.

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [July 23, 2008, 6:50am UTC](https://www.ruby-forum.com/t/how-do-i-open-a-terminal-using-ruby/143709/7 "2008-07-23T06:50:59Z")

</div>

On Tuesday 22 July 2008 03:54:46 Sebastian H. wrote:

> Tim P. wrote:
> 
> > Kernel.exec ‘/bin/bash’, ‘-c’, “ruby #{ **FILE** } #{ARGV.join(’ ')}”
> 
> That won’t open a terminal windows. It will simply execute bash which will  
> execute ruby, but if you weren’t in a window before, you’re still not.

I don’t think there’s any standard way to open another terminal  
_window_,  
given that some platforms allow you to install many different kinds of  
terminals in the first place, and there simply is no way to do this  
consistently and cross-platform.

On Windows, you could definitely call ‘cmd’. On OS X, you’re probably  
safe  
doing something with Terminal.app. But on Linux, what do you do?  
Gnome-terminal? Konsole? xterm? These are probably the most common to be  
installed, yet none are guaranteed…

You could always implement your own terminal, in your own GUI – but  
then  
you’ll irritate people who have a favorite terminal, and would rather  
work  
from there. So this only really makes sense if you’re just going to go  
whole-hog and use the GUI for everything.

I think about all you can do here is detect that you’re not running in a  
terminal, and if you’re not, exit with an error. Most platforms make it  
easy  
enough to run a script in a terminal – it’s the default on Windows; OS  
X has  
a “script menu” that opens them in Terminal; and on typical Linux  
desktop  
environments, application launchers have a simple checkbox for “run in  
terminal”, and double-clicking on a Ruby script should run it in a  
terminal  
anyway.

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [July 22, 2008, 11:00am UTC](https://www.ruby-forum.com/t/how-do-i-open-a-terminal-using-ruby/143709/8 "2008-07-22T11:00:53Z")

</div>

Tim P. wrote:

> Kernel.exec ‘/bin/bash’, ‘-c’, “ruby #{ **FILE** } #{ARGV.join(’ ')}”

That won’t open a terminal windows. It will simply execute bash which  
will  
execute ruby, but if you weren’t in a window before, you’re still not.
