You are not logged in.

#1 2012-03-19 19:04:26

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

[SOLVED]when to use environmental variables

Hi, I didn't see a 'general linux stuff' forum, so I'm posting here, hope you don't mind smile
I'd like a break down of the purpose of env variables, specifically when you should create your own.

Case in point: My prompt is getting a wee bit heavy (calling both ibam and acpi everytime the prompt is written).
So I figured: I'll have separate processes call these binaries and save the results somewhere.
Then my shell only needs to read those values, not call the binaries to produce them.
env variables was my first thought, and I'm convinced it is would be a simple and clean sollution, but I'm uncertain whether it might conflict with some unspoken purpose or guideline for env vars?

(I'm not completely sure how much more efficient it is, rewriting the zsh/bash function might be better, but bash/zsh Isn't my strongest, and it should provide some improvement.)

Last edited by Bladtman242 (2012-03-19 21:56:53)

Offline

#2 2012-03-19 19:16:46

wolfcore
Member
From: California
Registered: 2012-03-06
Posts: 137

Re: [SOLVED]when to use environmental variables

Bladtman242 wrote:

Case in point: My prompt is getting a wee bit heavy

How much is 'heavy'? Mind posting it?

https://wiki.archlinux.org/index.php/En … _Variables

If you want to monitor things like battery, why not use something like dzen?

Last edited by wolfcore (2012-03-19 19:20:16)

Offline

#3 2012-03-19 19:45:20

kazimir
Member
Registered: 2012-01-20
Posts: 81

Re: [SOLVED]when to use environmental variables

Just a suggestion, maybe what you're looking for is already exposed in the /proc or /sys hierarchy.

Offline

#4 2012-03-19 20:19:59

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: [SOLVED]when to use environmental variables

Wolfcore: of course, here it is: http://paste.pocoo.org/show/568176/

Well, I could use a widget, but I'd much rather make one. (Besides, while I really do appreciate ALL the help and advice I can get, I still want to know about env vars)
I'll definitely have a look at dzen though.

I read the wiki page you linked to, but it didn't quite provide the answer I was looking for.
When should a sysadmin/desktop user create his own env vars, and when should a programmer have a program make them? smile

KLazimir: Would you elaborate a little? smile
I'm not all that familiar with /proc (I actually thought it was deprecated).

EDIT: wrong link, still getting used to this hole "secondary and primary" buffer thingy big_smile

Last edited by Bladtman242 (2012-03-19 20:26:10)

Offline

#5 2012-03-19 20:33:48

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED]when to use environmental variables

I'd put the tmux stuff in .profile as you only need it when you login, not every time you launch a shell and the keybinds can be done in .inputrc

As for the battery stuff, you could have a script run in a loop and write to a file and then query the file from your PS1 - but if it is getting elaborate, I'd write it to a separate bar (like dzen as wolfcore suggested, or conky on your desktop -- depending what sort of DE/WM setup you have).


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2012-03-19 20:43:56

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: [SOLVED]when to use environmental variables

The tmux part is commented out, as I don't use it right now smile and I don't have a .profile file?
I'm actually okay with the shell, the only speed problem is with the RPROMPT, which is reloaded everytime the prompt is displayed.
I know widgets can do this stuff, but what I'd like to know is whether or not it is against convention to use environmental variables for this sort of thing.
What are their purpose, what need are they supposed to fulfil, for both admins, users, and programmers? smile

Offline

#7 2012-03-19 21:01:33

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED]when to use environmental variables

.profile could be .bash_profile (or whatever shell you decide to use)...

I think you are answering your own question (at least in terms of practical application): if the speed is an issue, move it out of the prompt.

In terms of a design perspective, as I understand it, the environmental variables are set for the session and will apply to all the shells in that session. There is no theoretical reason why you can't jam battery info into your PS1 -- to me, though, it doesn't seem very UNIXy (one thing well, etc). YMMV.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#8 2012-03-19 21:08:51

wolfcore
Member
From: California
Registered: 2012-03-06
Posts: 137

Re: [SOLVED]when to use environmental variables

Bladtman242 wrote:

Wolfcore: of course, here it is: http://paste.pocoo.org/show/568176/

Well, I could use a widget, but I'd much rather make one. (Besides, while I really do appreciate ALL the help and advice I can get, I still want to know about env vars)
I'll definitely have a look at dzen though.

Well, I'd say if you want to do-it-yourself, using what you've written to show battery info in your PS1 is really the same or close to it if you put it in Conky/Dzen.

Also (and as a separate point), you can put the battery stuff in a separate script and stick it in your crontab, making it run every 5 minutes for example, then save the output to a file or to an environmental variable and read from there. (instead of rerunning everytime your PS1 re-evals).

Offline

#9 2012-03-19 21:21:56

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: [SOLVED]when to use environmental variables

Thank you all very much, but I know how this can be done, I am simply not familiar with the philosophy of env vars. Is it bad form to fill stuff like this in there?

wolfcore: Thats exactly what I was thinking, except I'd have acpid set the "ac-adapter-plugged-in" variable, as to only run it when needed smile


Jasonwryab:I'm not sure I agree that its un-UNIXy.
Having a program do one thing, and one thing well, can include providing an overview. Which, in this case, for me, involves a battery indicator. The whole idea is that if we program does one thing well, other programs can use it, instead of having to do everything themselves.
It doesn't mean that one program can do only one thing, it means it should achieve one goal. Otherwise UNIX programmers could do nothing: Want to sort an array? Too bad, you said you program should be a battery applet. So what if we to make a nice graph of battery statistics? Leave it for another program? Well, you could have your program use gnuplot.

Thats how I think of it anyway smile

Anyway, to get back on topic, no matter if I want it in the prompt or not. The question is where to keep the values, if I want them accessible

Last edited by Bladtman242 (2012-03-19 21:26:54)

Offline

#10 2012-03-19 21:27:58

wolfcore
Member
From: California
Registered: 2012-03-06
Posts: 137

Re: [SOLVED]when to use environmental variables

There isn't a science/philosophy to environmental variables... just use them if you want to. Nothing is holding you back. They're used when you need to access the same variables from multiple places/applications... but it's not written in stone...

I'd say keeping your vars stored as environmentals is fine, just watch out you do not conflict with existing vars, and also (as an aside), as jasonwryan pointed out, you can move some of the things you're keeping in your .bashrc to your .bash_profile/.profile (which is only used at login).

Offline

#11 2012-03-19 21:35:20

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: [SOLVED]when to use environmental variables

Thanks smile
I confess I didn't quite catch the .profile thing earlier. (I searched for it in my home dir, but didn't find it)

I havent created a .zprofile file, but im guessing /etc/ for globals and ~/ for user specific?
(Using zsh as the login shell btw).

Could I put everything but the prompts in there? like aliases, keybindings, and zsh settings?

Last edited by Bladtman242 (2012-03-19 21:35:44)

Offline

#12 2012-03-19 21:49:11

wolfcore
Member
From: California
Registered: 2012-03-06
Posts: 137

Re: [SOLVED]when to use environmental variables

Bladtman242 wrote:

Thanks smile
I confess I didn't quite catch the .profile thing earlier. (I searched for it in my home dir, but didn't find it)

I havent created a .zprofile file, but im guessing /etc/ for globals and ~/ for user specific?
(Using zsh as the login shell btw).

Could I put everything but the prompts in there? like aliases, keybindings, and zsh settings?

As a rule of thumb:

  • Use ~/.bash_profile for things that apply to your whole session, including environmental variables.

  • Use ~/.bashrc for aliases, function definitions, shell options, and prompt settings.

  • key bindings normally go in ~/.inputrc

Also, if I'm not mistaken, there's a sample ~/.bash_profile, ~/.bashrc, etc in /etc/skel/

Offline

#13 2012-03-19 21:56:38

Bladtman242
Member
Registered: 2012-02-14
Posts: 127

Re: [SOLVED]when to use environmental variables

There is, I didn't know that.
Thanks again. marking thread as solved.

Offline

Board footer

Powered by FluxBB