LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-08-2009, 08:58 AM   #1
Rynn
LQ Newbie
 
Registered: Jun 2009
Posts: 3

Rep: Reputation: 0
bashrc and $COLUMNS


Hi,

I try to set a good Prompt in my bashrc and I need the value of $COLUMNS.

In many bashrc example in the web I saw this var used, but if I try to use it in my bashrc, it seems not to be set ... I don't know where I'm wrong ... how to get $COLUMNS set in bashrc ?

Thanks for you help
 
Old 06-08-2009, 09:38 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As far as I've been able to tell, you can't access the value of $COLUMNS directly from settings created in bashrc. This is probably because bashrc is used only for the shell's initial settings. You can however, create a separate script, and pass the value to it from the outside in the form of a command argument.

Back at the beginning of the year I posted a script I wrote that does pretty much the same thing as you want--it uses the $COLUMNS variable in setting the PS1 prompt. I'm sure you can adapt yours to use the same basic concept.
 
Old 06-08-2009, 10:14 AM   #3
Rynn
LQ Newbie
 
Registered: Jun 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Very interesting script. I'll try to do the same way for my own purpose, thanks.

However why do i saw many bashrc in the web which used $COLUMNS like if it was usable in it ?
 
Old 06-08-2009, 02:00 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'd like to see an example of that. Maybe there's some way to export the variable or something I don't know about.

Truthfully, I really don't know why it doesn't work. I wasn't able to figure it out when I was experimenting with it. Nothing I tried ever got me to get the variable value from within bashrc. It must have something to do with the way they're made (or not made) available to the shell or subshells or something.
 
Old 06-08-2009, 04:25 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Well, you piqued my curiosity again, so I've been spending some time playing around.

It seems that you CAN use the $COLUMNS variable inside of aliases and functions, and it will read the correct value every time.

But any time you try to read that value into a variable, including shell variables like PS1, it will always read as the value the terminal started at. It will not register the current value after changing the size. You can create a perfectly good function that properly echoes the value of $COLUMNS every time you run it, for example, but if you try to use that function to set the value of a variable inside bashrc, it fails. I suppose that it sets the variables at the time of parsing the file, and so the values become fixed.

I tried everything I can think of to get around this, using eval, indirect variable referencing, command substitution...nothing I try seems to affect it. I can find no way to make it re-set the variable value every time from within bashrc itself.
 
Old 06-08-2009, 04:38 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
One example is worth more than a thousand words:

Code:
$ PROMPT_COMMAND='PS1="Number of columns: $(tput cols)__"'
Number of columns: 183__
Number of columns: 183__
Number of columns: 183__
Number of columns: 129__
Number of columns: 129__
Number of columns: 129__
Number of columns: 85__
Number of columns: 85__
Number of columns: 85__
 
Old 06-08-2009, 04:48 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
<headslap>

D'oh! I just figured out what I was doing wrong, and it's something I had discovered
before and forgot. You have to use single quotes around the prompt value. That lets it set the value dynamically (I'm not sure why though. Probably it needs to parse the value each time or something).

I was only just reminded of it while looking over the code I wrote before.

But of course I was beaten to the punch. Just forget everything I said up to now.
 
Old 06-08-2009, 05:19 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by David the H. View Post
<headslap>

D'oh! I just figured out what I was doing wrong, and it's something I had discovered
before and forgot. You have to use single quotes around the prompt value. That lets it set the value dynamically
It's the other way around. When you use single quotes no expansion is possible. That's why you need to use them, so the command is stored there instead of the interpreted value, which in turn allow it to change dynamically. The contents of PROMPT_COMMAND value is ran just before drawing each new prompt. So, with the lines I posted above:

Code:
PROMPT_COMMAND='PS1="Number of columns: $(tput cols)__"'
The contents of $PROMPT_COMMAND will be *exactly* this (and when I say "exactly" I mean exactly, including the quotation marks:

Code:
PS1="Number of columns: $(tput cols)__"
This command will be run each time a new prompt is to be drawn, which in turn makes your prompt dynamic. On the contrary if I had used this (double quotes):

Code:
PROMPT_COMMAND="PS1=\"Number of columns: $(tput cols)__\""
Then it wouldn't work, it will always use the original value. This is because when running that from bashrc expansion will take place (because double quotes don't prevent command nor variable expansion. Hence, the value stored in COMMAND_PROMPT will be this:

Code:
PS1="Number of columns: 183__"
You can try yourself in command line:

Code:
$ PROMPT_COMMAND='PS1="Number of columns: $(tput cols)__"'
Number of columns: 183__#<<- this is the initial value
Number of columns: 183__#<<- now I resize my terminal
Number of columns: 183__#
Number of columns: 152__#<<-as you see, the value changed
Number of columns: 152__#<<-this is the contents of PROMPT_COMMAND
Number of columns: 152__echo $PROMPT_COMMAND
PS1="Number of columns: $(tput cols)__"
Number of columns: 152__#<<-now I re-set it using double quotes instead
Number of columns: 152__PROMPT_COMMAND="PS1=\"Number of columns: $(tput cols)__\""
Number of columns: 152__#<<-and resize the terminal again
Number of columns: 152__
Number of columns: 152__#as you see, the value didn't change, now it's hardcoded
Number of columns: 152__echo $PROMPT_COMMAND
PS1="Number of columns: 152__"
To sum up, single quotes prevent expansion, so the command is run every time the prompt needs to be drawn. On the contrary, double quotes don't prevent command expansion, so it's evaluated once only, when bashrc is run.

Last edited by i92guboj; 06-08-2009 at 05:24 PM.
 
Old 06-09-2009, 02:20 AM   #9
Rynn
LQ Newbie
 
Registered: Jun 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Woah !
Thanks a lot for spending time playing with my problem, all of your post was very interesting and will help me a lot.

I'll post later the result of my prompt

Last edited by Rynn; 06-09-2009 at 02:34 AM.
 
Old 06-09-2009, 03:52 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by i92guboj View Post
It's the other way around. When you use single quotes no expansion is possible. That's why you need to use them, so the command is stored there instead of the interpreted value, which in turn allow it to change dynamically.
That's what I meant. I just didn't say it very well. I was trying to say that the string must be protected until it's time for the command to be actually run.

But I'm glad now that I've got everything cleared up. I see I was laboring under some misconceptions from my previous experiences. Now that I know what the hang-up was I've been able to move my previous script into a function inside bashrc and don't have to call a separate file anymore.

I've also spent some time spicing up my prompt with colors too. And just to help out anyone else trying the same thing, it took me a while before I discovered that you have to enclose the escape sequences in \[ and \] or else bash gets confused about newlines. Arch Linux has a really good wiki page about prompt customization that helped me out there.
 
Old 06-09-2009, 04:12 PM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by David the H. View Post
That's what I meant. I just didn't say it very well. I was trying to say that the string must be protected until it's time for the command to be actually run.
Well, it's a bit hard to explain it in a clear way even if you know how it works. I don't think I did a particularly good job at explaining it either, I'd rather think that our posts complement each other so hopefully anyone reading them both in this context might understand how it truly works.

Last edited by i92guboj; 06-09-2009 at 04:13 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ls columns twelvenine Linux - Newbie 6 12-21-2008 01:33 PM
Setting path: /etc/profile, /etc/bashrc or ~/.bashrc Swakoo Linux - General 1 08-07-2007 10:59 PM
Need Help With Columns Post Modern Programming 2 02-01-2006 11:18 AM
Vi Columns Chaitanyayardi Linux - Software 5 05-12-2005 07:27 AM
How can I see the other 10 columns sakulagi Linux - Software 1 06-16-2003 09:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration