getenv Not Working for COLUMNS and LINES
c, environment-variables, getenv
Solution
`COLUMNS` and `LINES` are set by the shell, but not exported, which means that they are not added to the environment of subsequently executed commands. (To verify that, examine the output of `/usr/bin/env`: it will show `PATH` and `USER`, but not `COLUMNS` and `LINES`.)
In the bash shell, you can call `export VAR` to mark a variable for export.
Alternatively, see Getting terminal width in C? for various ways to obtain the terminal width and height programmatically.
Problem
I am trying to get the number of columns and lines in my program. I am using the following code to do so: ``` ... char *cols = getenv("COLUMNS"); printf("cols: %s\n", cols); char *lines = getenv("LINES"); printf("lines: %s\n", lines); ... ``` The problem is that when I run this I get null for both. Running this with other environment variables, such as `PATH` or `USER`, works fine. What I find strange is that running `echo $COLUMNS` and `echo $LINES` from the same shell both work fine. Why is my program unable to get these two environment variables.