# ENV values

**URL:** <https://www.ruby-forum.com/t/env-values/66914>\
**Category:** Ruby\
**Created:** [August 30, 2006, 9:15am UTC](https://www.ruby-forum.com/t/env-values/66914 "2006-08-30T09:15:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [August 30, 2006, 9:15am UTC](https://www.ruby-forum.com/t/env-values/66914/1 "2006-08-30T09:15:24Z")

</div>

Hi all,

I’m working on an ssh script where I’d like to read out console  
dimensions to adjust the output window - the default value of 80x24  
isn’t that good. 😉

So I thought it should work by simply reading out the environment  
variables $COLUMNS and $LINES (Linux system). The results in irb were  
what I needed:

===—===—===—===—===—===  
irb(main):001:0\> ENV[‘COLUMNS’]  
=\> “99”  
irb(main):002:0\> ENV[‘LINES’]  
=\> “32”  
===—===—===—===—===—===

But in a ruby script these things didn’t work anymore. A really simple  
script:

===—===—===—===—===—===  
#!/usr/bin/ruby -w

puts ENV[‘USER’]  
puts ENV[‘LINES’]  
===—===—===—===—===—===

This gave me the output:  
frank  
nil

Ok, that didn’t work like I’ve expected it, so here is another way:

===—===—===—===—===—===  
irb(main):001:0\> lines = `echo $LINES`  
=\> “32\n”  
irb(main):002:0\> puts lines  
32  
=\> nil  
===—===—===—===—===—===

Now as ruby script:

===—===—===—===—===—===  
!/usr/bin/ruby -w

lines = `echo $LINES`  
puts lines  
===—===—===—===—===—===

Well, also no success. :-/ It gave me only a blank line.

Does anyone have an idea what I could do to get column & line numbers of  
the console window?

Thanks…

Frank

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [August 30, 2006, 10:22am UTC](https://www.ruby-forum.com/t/env-values/66914/2 "2006-08-30T10:22:22Z")

</div>

Frank H. wrote:

/ …

> Does anyone have an idea what I could do to get column & line numbers of  
> the console window?

Ruby doesn’t know about the “console window” that spawned it. The  
console  
window is the parent process of the Ruby process, and the child process  
doesn’t inherit all of the parent’s environment variables. One reason  
for  
this is that you can change the console window size as Ruby runs, but  
Ruby  
can’t get the window size changes from its parent.

So, because they cannot be relied on to be correct in the child process,  
these two values are simply dropped from the ENV list.

Try this experiment in a Linux desktop environment with an open Konsole  
or  
other command window:

$ while true; do echo “$LINES,$COLUMNS”; usleep 100; done

Now change the window size as the above code line runs. See the numbers  
change? Ruby cannot keep track of these changes in its parent.

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [August 30, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/env-values/66914/3 "2006-08-30T12:36:50Z")

</div>

Hi Paul, hi Tom,

thanks for your hints. 🙂  
The ncurses solution didn’t work, WINDOW method isn’t known.  
But no problem, I’m glad about every idea. 🙂

In the meanwhile I’ve solved the problem by using `stty`.

===—===—===—===—===—===  
#!/usr/bin/ruby -w  
c = `stty size`.chomp  
c =~ /\s/  
lines, columns = $`, $’  
===—===—===—===—===—===

Frank

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [August 31, 2006, 4:12am UTC](https://www.ruby-forum.com/t/env-values/66914/4 "2006-08-31T04:12:45Z")

</div>

On Wednesday 30 August 2006 19:37, Frank H. wrote:

> c = `stty size`.chomp  
> c =~ /\s/  
> lines, columns = $`, $’  
> ===—===—===—===—===—===

or, to make it a little bit more readable (no offend, but yours looks  
like  
perl 😉

===—===—===—===—===—===  
lines, columns = `stty size`.chomp.split

# [“81”, “226”]

===—===—===—===—===—===

thanks for finding that out btw, i love it 🙂

MfG  
manveru

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [August 30, 2006, 11:36am UTC](https://www.ruby-forum.com/t/env-values/66914/5 "2006-08-30T11:36:42Z")

</div>

On 8/30/06, Frank H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ===—===—===—===—===—===  
> #!/usr/bin/ruby -w
> 
> ===—===—===—===—===—===
> 
> Thanks…
> 
> Frank
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

## If you dont mind depending on ncurses then will something like this work? you will need the ncurses-ruby package

require “ncurses.rb”

## win = Ncurses.WINDOW.new() win.getmaxyx(y=[], x=[]) win.delwin()

This should open ncurses mode, get its dimensions - which are handled  
correctly and then drop you back out of ncurses.

Note that I havent tried it at all, am a complete newb to Ruby and  
found the bits via google, but for 4 lines its worth a try.

Tom
