$COLUMNS aren't set

Discussion in general that pertains to Sabayon Linux - Must Pertain to Sabayon Linux

Moderator: Moderators

$COLUMNS aren't set

Postby yokmp » Mon May 26, 2014 4:37

I know, it's not rly a problem. But before you can use anything like
Code: Select all
printf "%*s\n" $(((${#TEXT} + $COLUMNS)/2)) "$TEXT"

to center some Text in your .bashrc, you must do an
Code: Select all
export COLUMNS

Else the variable is unset/empty. AFTER the Terminal is loaded (or you're logged in and see your PS1) the COLUMNS variable is set. This is since Sabayon 4 or 5. Not shure about this. Its just annoying.
So if you know a better solution than export this in the .bashrc, please tell me.
Last edited by yokmp on Mon May 26, 2014 11:24, edited 1 time in total.
yokmp
Young Hen
 
Posts: 26
Joined: Sun Apr 26, 2009 18:28

Re: $COLUMNS aren't set

Postby svantoviit » Mon May 26, 2014 8:54

Use tput in your script: BashFAQ/091: I'm trying to get the number of columns or lines of my terminal but the variables COLUMNS / LINES are always empty.

Here is a working example (after correcting your small typo):
Code: Select all
#!/bin/bash
TEXT="TEXT"
COLUMNS=$(tput cols)
printf "%*s\n" $(((${#TEXT} + $COLUMNS)/2)) "$TEXT"
svantoviit
Old Dear Hen
 
Posts: 651
Joined: Sun Feb 28, 2010 17:55

Re: $COLUMNS aren't set

Postby yokmp » Mon May 26, 2014 11:35

Which typo? ^^
I know about tput but why use a programm when the Information should be in a global var? Plus the fact that not every Distro has tput onboard. So you have to catch that case and provide a fallback or just don't use tput and use the global. Sabayon is (as far as i know) the only Distro where its not set.
It's my personal fallback in Sabayon and a reason to switch to some exotic linux alpha :P
yokmp
Young Hen
 
Posts: 26
Joined: Sun Apr 26, 2009 18:28

Re: $COLUMNS aren't set

Postby svantoviit » Mon May 26, 2014 12:36

yokmp wrote:Which typo?

In the original post you wrote:
Code: Select all
$#{TEXT}
instead of
Code: Select all
${#TEXT}
I see you've edited it.

yokmp wrote:the Information should be in a global var?

It is set in an interactive shell, that's the way it works… just read the link I posted above.

yokmp wrote:not every Distro has tput onboard

It's part of ncurses which is a pretty standard package.

yokmp wrote:Sabayon is (as far as i know) the only Distro where its not set.

Sure? Don't think so.

yokmp wrote:So you have to catch that case and provide a fallback

Don't fool yourself and others ;)

… you are welcome!
svantoviit
Old Dear Hen
 
Posts: 651
Joined: Sun Feb 28, 2010 17:55

Re: $COLUMNS aren't set

Postby yokmp » Mon May 26, 2014 20:21

[quote]It is set in an interactive shell, that's the way it works[/quote
Yes it is, and the script is only called when the shell is interactive:
Code: Select all
if [[ $- == *i* ]]; then # if interactive, then
  export COLUMNS=80 # workaround
  source ~/script.sh
fi


I would like to know is WHY it worked a while back (maybe 2 years? not shure about this)?
I mean there must be a reason they've changed it. If I can't break anything i would like to write the 'export line' in /etc/skel/.bashrc (?)
yokmp
Young Hen
 
Posts: 26
Joined: Sun Apr 26, 2009 18:28


Return to Sabayon Linux General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests

cron