LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-06-2008, 03:20 PM   #1
sagsriv
LQ Newbie
 
Registered: Jan 2008
Location: Kolkata India
Distribution: fedora 6
Posts: 21

Rep: Reputation: 15
change PS1 environment variable from c program


I am trying to create a new shell which is supposed to show some prompt other than usual one. I need to know how to change the PS1 variable from a C program i.e. the C equivqlent of the shell command PS1="Something new >".
 
Old 03-06-2008, 03:27 PM   #2
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
setenv or putenv
 
Old 03-06-2008, 03:44 PM   #3
sagsriv
LQ Newbie
 
Registered: Jan 2008
Location: Kolkata India
Distribution: fedora 6
Posts: 21

Original Poster
Rep: Reputation: 15
getenv and putenv fails

Well I tried getenv() and putenv() to find the value of env. variable PS1 and change it. But getenv() shows there variable PS1 hs no value. I give the code here.

/*code to change PS1*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *var, *value;
if(argc == 1 || argc > 3) {
fprintf(stderr,"usage: environ var [value]\n");
exit(1);
}
var = argv[1];
value = getenv(var);
if(value)
printf("Variable %s has value %s\n", var, value);
else
printf("Variable %s has no value\n", var);

if(argc == 3) {
char *string;
value = argv[2];
string = malloc(strlen(var)+strlen(value)+2);
if(!string) {
fprintf(stderr,"out of memory\n");
exit(1);
}

strcpy(string,var);
strcat(string,"=");
strcat(string,value);
printf("Calling putenv with: %s\n",string);
if(putenv(string) != 0) {
fprintf(stderr,"putenv failed\n");
free(string);
exit(1);
}

value = getenv(var);
if(value)
printf("New value of %s is %s\n", var, value);
else
printf("New value of %s is null??\n", var);
}
exit(0);
}

Now when I compile and run my file getputenv with ./getputenv PS1 "Something new >" it gives following output
./getputenv PS1 "Something new >"
Variable PS1 has no value
Calling putenv with: PS1=Something new >
New value of PS1 is Something new >

But the same code works fine with 'PATH' variable.

Another thing, whenever the program exits, variable PATH regains its previous value. But for PS1, I want to keep the prompt changed as long as the shell runs. How can I do that?
 
Old 03-08-2008, 01:26 PM   #4
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
Quote:
Another thing, whenever the program exits, variable PATH regains its previous value. But for PS1, I want to keep the prompt changed as long as the shell runs. How can I do that?
Sorry, I misunderstood your original request. You cannot do this in unix, it is not possible.

OK, this is a common problem that many new users unfamiliar with unix processes seem to have. It's straightforward once you get the hang of it though.

Every process inherits its environment from its parent process. That is, it gets a _copy_ of the parent's environment. The child can change its own environment any way it likes but it *cannot*, ever, in any way, affect the parent's environment. That is why variables return to their original value once the program exits. In fact, they don't really "return" to the old value, because they never really changed, all your process did was change its own copy of the variable, the variable in the parent's environment did not change.

A good book or tutorial on unix programming will explain this in more detail.

Quote:
Variable PS1 has no value
If PS1 has no value, then it has no value. Assuming PS1 is set to something in your shell, which presumably it is, the only logical conclusion I can think of is that PS1 is not being exported. Environment variables can be local to the shell (a bit like a local variable in a C function, which can't be accessed from other functions, or in this case, processes) or exported, which means that it will be passed in the environment to child processes. Put 'export PS1' somewhere in your ~/.bashrc (or the appropriate rc file if you don't use bash), that should fix it.

If you want to change PS1 (or any other variable) in the shell, then the shell must change it itself, as explained above, other programs cannot do this. I.e. use a shell function or an alias or *source* (not run in a sub-shell) a shell script.
 
Old 03-10-2008, 02:39 PM   #5
sagsriv
LQ Newbie
 
Registered: Jan 2008
Location: Kolkata India
Distribution: fedora 6
Posts: 21

Original Poster
Rep: Reputation: 15
Thanx ararus

I got what I needed. Thank you.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PS1 environment variable linuxjamil Linux - Networking 1 06-17-2007 12:51 AM
Prompt not hard returning after PS1 variable change? SlowCoder Linux - Newbie 5 05-01-2007 10:16 PM
Environment Variable: how to change env DISPLAY in Redhat 9 lucastic Linux - General 2 07-21-2004 05:15 PM
PS1 variable wogga Red Hat 2 05-26-2004 06:39 PM
setting the PS1 enviroment variable in xterm schatoor Linux - General 2 04-29-2003 09:41 AM


All times are GMT -5. The time now is 10:27 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration