Accessing bash local environment variables in C

Accessing bash local environment variables in C

Post by Larry Gene Gariepy Jr » Sat, 06 Dec 1997 04:00:00



Hello,
     I am new to this list, and I am not completely sure that this
message doesn't belong on comp.lang.c.

     I am trying to write a C program to operate a display on an xterm,
and as such I would like to read the environment variables COLUMNS and
LINES to know what size window I am dealing with.  I am running Debian
Linux on my home PC, and hence I use the bash shell.  The problem with
bash is that the COLUMNS and LINES environment variables are not
exported to the shell environment.   Hence a call to getenv() does not
find them.  I realize that perhaps using system("export COLUMNS") and
then searching the environment might work, but this seems like a hack,
and I am not sure I want my program to go around modifying its parent's
environment.  Is there anyway to get at these local environment
variables without exporting them?

Thanks in advance,
Larry Gariepy

 
 
 

Accessing bash local environment variables in C

Post by Andrew Giert » Sat, 06 Dec 1997 04:00:00


 Larry> Hello,
 Larry>      I am new to this list, and I am not completely sure that
 Larry> this message doesn't belong on comp.lang.c.

I think you made the right choice.

 Larry>      I am trying to write a C program to operate a display on
 Larry> an xterm, and as such I would like to read the environment
 Larry> variables COLUMNS and LINES to know what size window I am
 Larry> dealing with.  I am running Debian Linux on my home PC, and
 Larry> hence I use the bash shell.  The problem with bash is that the
 Larry> COLUMNS and LINES environment variables are not exported to
 Larry> the shell environment.  Hence a call to getenv() does not find
 Larry> them.

 Larry>  I realize that perhaps using system("export COLUMNS") and
 Larry> then searching the environment might work

It doesn't.

 Larry> but this seems like a hack, and I am not sure I want my
 Larry> program to go around modifying its parent's environment.

You can't do that anyway.

 Larry> Is there anyway to get at these local environment variables
 Larry> without exporting them?

No.

You have two main approaches. One is to export LINES and COLUMNS in
the shell first (they should always be exported, really).

The other approach is to get the window size from the tty device
using TIOCGWINSZ.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

Accessing bash local environment variables in C

Post by Lars Kellogg-Stedm » Mon, 08 Dec 1997 04:00:00


Quote:>exported to the shell environment.   Hence a call to getenv() does not
>find them.  I realize that perhaps using system("export COLUMNS") and

It seems to be that the simplest solution would be to put:

  export COLUMNS LINES

in your .profile or .bashrc.  Poof, they'll always be in the environment.
Alternately, you could find the code to the resize command, which queries
the terminal screen size.

-- Lars

--

Office of Information Technology, Boston University
PGP 768/4E444575 5F 6C 2C 3E 31 D4 E7 15  E4 33 10 98 6A 14 8B F6

 
 
 

1. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

Is there a proper fixed bash on any of the FTP sites out there?

I know there bash is on the usual sites but I don't know if they are
bugged or not :(

Regards,

Neil.

--


------------------------------------| Edinburgh, EH14 2DE, United Kingdom
**Domino: There`s nothing you can do when you`re the next in line: Domino**

2. Message Queue + Sockets Multiplexing

3. Testing Whether an environment variable contains integer data in BASH

4. Unix I/O driver

5. BASH: environment variable in function returns empty

6. Looking at Seagate TR-4 or Sony SDT-7000 DDS-2

7. bash : how to reset environment variable settings

8. Where is psaux.c ?

9. increasing the # of environment variables (in bash)

10. Where do I put environment variables in the Bash shell?

11. Shell versus environment variables in bash.

12. -= Environment variables using bash =-

13. Setting environment variables in BASH