pterm KSH environment variable inheritance mystery solved

I’m posting this at the prompting of another QNX user (It’s qrm’s fault)

I’ve had the most terrible time trying to understand where to put
environment variables such that when I run a pterm they show up. Especially
the PS1 variable.

Seems there are a few others that don’t understand either. Well…if this
is you, this is your lucky day.

First some basics:

  1. When you login, the following occurs (there may be more stuff, but this
    should be sufficient for this discussion.)
    a. The $HOME variable is set from your home dir entry in the passwd
    file (make sure this directory actually exists and that it is owned by you
    and at least one of the groups to which you belong).
    b. /etc/profile is run. This is a script MAKE SURE it has execute
    permissions.
    c. $HOME/.profile script is run…make sure this also has execute
    permissions (these two have been problems for me and others)
    d. A shell is started.
    e. Before the shell gives you a prompt, it looks for the environment
    var ‘ENV’, if set with a proper file name, will try to execute the file as a
    script.

  2. One thing occurs when /etc/profile is run that is interesting. There is
    a bit of code in there that attempts to set the $ENV variable.
    This is set if the name used to start your shell (with a ‘.’ in front)
    exists as a script (with execute permissions) in your home directory. So,
    if you use ksh and it was started using sh, and you are ‘root’, and you have
    created a /root/.sh file (with execute permissions) ENV will be set to
    ‘/root/.sh’. Thus when a shell is invoked, that script will be run.
    This is important because there are some environment variables that get
    overridden when the shell starts (i.e. PS1), so in order to get these, you
    need to have a ‘.sh’ file that will set it for you. It isn’t good enough
    for them to be in the $HOME/.profile script.

Now, what does this have to do with pterm?

Well…funny you should ask.

If you use the graphical login, it will run /etc/profile and
$HOME/.profile…you just won’t see it. Therefore it is important that if
you want to set a environment variable like ‘PS1’, you will need to create a
…sh file (or .bash if you are using bash (I think, I don’t use bash, so I
can’t say)) in your home directory and give it execute permissions. Put the
required ‘export PS1=blah blah blah’ in .sh.

Then when you start a pterm, the ‘.sh’ file will be executed and any
shifting environment variables can be set.

Note that there are many environment variables that will be inherited by the
shell from the execution of /etc/profile and $HOME/.profile (when you logged
into photon), so you don’t need to load ‘.sh’ with redundant stuff…


Hope this was useful. (no complaining about the bad grammar, I was in a
hurry!),

Kevin

Kevin Stallard <kevin@ffflyingrobots.com> wrote:

I’m posting this at the prompting of another QNX user (It’s qrm’s fault)

I’ve had the most terrible time trying to understand where to put
environment variables such that when I run a pterm they show up. Especially
the PS1 variable.

Seems there are a few others that don’t understand either. Well…if this
is you, this is your lucky day.

First some basics:

  1. When you login, the following occurs (there may be more stuff, but this
    should be sufficient for this discussion.)
    a. The $HOME variable is set from your home dir entry in the passwd
    file (make sure this directory actually exists and that it is owned by you
    and at least one of the groups to which you belong).
    b. /etc/profile is run. This is a script MAKE SURE it has execute
    permissions.
    c. $HOME/.profile script is run…make sure this also has execute
    permissions (these two have been problems for me and others)
    d. A shell is started.

NOPE. Wrong order here.

login sets the $HOME environment variable.

It then runs a shell with argv[0][0]=’-’

try a “sin arg”, you will see something like:
563 dagibbs -sh

The shell then does its initialization.

Shell initialization rules:

if argv[0][0] is ‘-’ then this is a “login shell”
{
– include & evaluate the shell commands in /etc/profile (if it exists)
– include & evaluate the shell commands in $HOME/.pofile (if it exists)
}
check for the ENV environment variable, if it is set:
– include & evaluate the shell commands in $ENV (if it exists)

Traditionally the file ENV points to is $HOME/.rc, e.g.
/home/dagibbs/.kshrc or /home/dagibbs/.shrc

Note: environment variables are inheritted from parent to child, but
aliases and shell functions are not. So, generally, you should export
environment variables in your .profile, but set aliases or declare shell
functions you want to use in your .shrc (ENV target) file.

e. Before the shell gives you a prompt, it looks for the environment
var ‘ENV’, if set with a proper file name, will try to execute the file as a
script.



Now, what does this have to do with pterm?

Well…funny you should ask.

If you run “pterm -l” it will start your shells as a “login shell” and
you will get all the normal startup that you expect.

This is generally the easiest and cleanest way of getting a consistent
environment in your pterm shells, and the one I use.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David Gibbs <dagibbs@qnx.com> wrote:

If you run “pterm -l” it will start your shells as a “login shell” and
you will get all the normal startup that you expect.

This is generally the easiest and cleanest way of getting a consistent
environment in your pterm shells, and the one I use.

That’s debatable. (I mean the “easiest and cleanest” part, not the “the
one I use” part, of course…)

Is there something that you can do using “pterm -l” that you can’t do
easily using $ENV? (The only thing I can think of is the .lastlogin
message at startup, and I don’t see why anyone would actually want that
in all their pterms…)

Adding a line that says “export ENV=$HOME/.kshrc” to your .profile and
moving all your shell functions and aliases from .profile to .kshrc
seems pretty easy and clean to me.

On the other hand, making sure that all the places that can potentially
run a pterm for you, like shelf and pwm, run pterm with the -l option,
feels more messy to me. I don’t like the idea of having to remember to
type the extra " -l" every time I start a pterm from another pterm,
either.

Also, notice that $ENV works for all your shells. If you run vi from
a shell and then another shell from the vi, the new shell will run your
$ENV file if you have one; but there’s no way to tell vi to spawn a
login shell to make it run your .profile. (I know: you can tell vi to
run pterm -l instead of just a shell. But what if you’re in text mode?)

\

Wojtek Lerch QNX Software Systems Ltd.

snip

First some basics:

  1. When you login, the following occurs (there may be more stuff, but
    this
    should be sufficient for this discussion.)
    a. The $HOME variable is set from your home dir entry in the passwd
    file (make sure this directory actually exists and that it is owned by
    you
    and at least one of the groups to which you belong).
    b. /etc/profile is run. This is a script MAKE SURE it has execute
    permissions.
    c. $HOME/.profile script is run…make sure this also has execute
    permissions (these two have been problems for me and others)
    d. A shell is started.

NOPE. Wrong order here.

login sets the $HOME environment variable.
I’m sure it does, but it uses the entry in your passwd file. So I’m

technically still correct.

It then runs a shell with argv[0][0]=’-’

try a “sin arg”, you will see something like:
563 dagibbs -sh

The shell then does its initialization.

Shell initialization rules:

if argv[0][0] is ‘-’ then this is a “login shell”
{
– include & evaluate the shell commands in /etc/profile (if it
exists)
– include & evaluate the shell commands in $HOME/.pofile (if it
exists)
}
check for the ENV environment variable, if it is set:
– include & evaluate the shell commands in $ENV (if it exists)

Traditionally the file ENV points to is $HOME/.rc, e.g.
/home/dagibbs/.kshrc or /home/dagibbs/.shrc
/etc/profile sets ENV. (At least under QNX 6) The following lines can be

found there:
if test -z “$ENV”; then;
ENV=~/.$(SHELL##*/}
if test -x $ENV; then
export ENV
else
unset ENV
fi
fi

So…like I said, under QNX 6.x, this $ENV will be .sh and not .kshrc or
…shrc like it was in QNX 4.2x
(If the shell you are running is ‘sh’)


Note: environment variables are inheritted from parent to child, but
aliases and shell functions are not. So, generally, you should export
environment variables in your .profile, but set aliases or declare shell
functions you want to use in your .shrc (ENV target) file.

e. Before the shell gives you a prompt, it looks for the
environment
var ‘ENV’, if set with a proper file name, will try to execute the file
as a
script.


Now, what does this have to do with pterm?

Well…funny you should ask.

If you run “pterm -l” it will start your shells as a “login shell” and
you will get all the normal startup that you expect.

This is generally the easiest and cleanest way of getting a consistent
environment in your pterm shells, and the one I use.
However you neglect to see that /etc/profile and $HOME/.profile are run by

phlogin, thus setting ENV variables that can be inherited before any pterm
is run. Thus you don’t have to go and change any refrences to the laucning
of pterm and add a -l to the invocation.

This was my whole point. It all will work as it comes w/o invocting pterm
with the -l.

If you don’t belive me, do the following:
Remove any pterm invoking with the -l (i.e. from the shelf)
Insure your $HOME/.profile has some crazy env variable that it sets.
I.E. export SCOOBY=12345
Logoff photon
At the graphical photon login screen…login (don’t just type ph from
your text login in other words)
Open a pterm
type: echo $SCOOBY

If .profile is in the correct place and has exceute premissions, you
will see
12345

All w/o adding the -l to pterm.

The reason I’m so insistant with this is that I thought that the -l would
necessary as well, but I want to point out that the default settings do
work and no change to them are necessary (i.e. modifying the pterm shelf
item and adding -l after you install QNX 6.x).


-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Ahem…after reading this over…I don’t like it’s tone. Sorry if I
sound condescending, I don’t mean to.

Kevin

“Kevin Stallard” <kevin@ffflyingrobots.com> wrote in message
news:a91lm6$nsa$1@inn.qnx.com

snip

First some basics:

  1. When you login, the following occurs (there may be more stuff, but
    this
    should be sufficient for this discussion.)
    a. The $HOME variable is set from your home dir entry in the
    passwd
    file (make sure this directory actually exists and that it is owned by
    you
    and at least one of the groups to which you belong).
    b. /etc/profile is run. This is a script MAKE SURE it has
    execute
    permissions.
    c. $HOME/.profile script is run…make sure this also has execute
    permissions (these two have been problems for me and others)
    d. A shell is started.

NOPE. Wrong order here.

login sets the $HOME environment variable.
I’m sure it does, but it uses the entry in your passwd file. So I’m
technically still correct.


It then runs a shell with argv[0][0]=’-’

try a “sin arg”, you will see something like:
563 dagibbs -sh

The shell then does its initialization.

Shell initialization rules:

if argv[0][0] is ‘-’ then this is a “login shell”
{
– include & evaluate the shell commands in /etc/profile (if it
exists)
– include & evaluate the shell commands in $HOME/.pofile (if it
exists)
}
check for the ENV environment variable, if it is set:
– include & evaluate the shell commands in $ENV (if it exists)

Traditionally the file ENV points to is $HOME/.rc, e.g.
/home/dagibbs/.kshrc or /home/dagibbs/.shrc
/etc/profile sets ENV. (At least under QNX 6) The following lines can be
found there:
if test -z “$ENV”; then;
ENV=~/.$(SHELL##*/}
if test -x $ENV; then
export ENV
else
unset ENV
fi
fi

So…like I said, under QNX 6.x, this $ENV will be .sh and not .kshrc
or
.shrc like it was in QNX 4.2x
(If the shell you are running is ‘sh’)



Note: environment variables are inheritted from parent to child, but
aliases and shell functions are not. So, generally, you should export
environment variables in your .profile, but set aliases or declare shell
functions you want to use in your .shrc (ENV target) file.

e. Before the shell gives you a prompt, it looks for the
environment
var ‘ENV’, if set with a proper file name, will try to execute the
file
as a
script.


Now, what does this have to do with pterm?

Well…funny you should ask.

If you run “pterm -l” it will start your shells as a “login shell” and
you will get all the normal startup that you expect.

This is generally the easiest and cleanest way of getting a consistent
environment in your pterm shells, and the one I use.
However you neglect to see that /etc/profile and $HOME/.profile are run
by
phlogin, thus setting ENV variables that can be inherited before any
pterm
is run. Thus you don’t have to go and change any refrences to the
laucning
of pterm and add a -l to the invocation.

This was my whole point. It all will work as it comes w/o invocting pterm
with the -l.

If you don’t belive me, do the following:
Remove any pterm invoking with the -l (i.e. from the shelf)
Insure your $HOME/.profile has some crazy env variable that it sets.
I.E. export SCOOBY=12345
Logoff photon
At the graphical photon login screen…login (don’t just type ph from
your text login in other words)
Open a pterm
type: echo $SCOOBY

If .profile is in the correct place and has exceute premissions, you
will see
12345

All w/o adding the -l to pterm.

The reason I’m so insistant with this is that I thought that the -l would
necessary as well, but I want to point out that the default settings do
work and no change to them are necessary (i.e. modifying the pterm shelf
item and adding -l after you install QNX 6.x).



-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

It is a confusing issue. In mean, who does what and in what order and what
takes precedence over a conflicting setting done elsewhere.

Is it also dealt with inadequately in the docs.

Like, where does /etc/default/login come in (or is there one in QNX6, I
actually haven’t checked yet)?


“Kevin Stallard” <kevin@ffflyingrobots.com> wrote in message
news:a91m9s$o8q$1@inn.qnx.com

Ahem…after reading this over…I don’t like it’s tone. Sorry if I
sound condescending, I don’t mean to.

Kevin

“Bill Caroselli (Q-TPS)” <QTPS@earthlink.net> wrote:

It is a confusing issue. In mean, who does what and in what order and what
takes precedence over a conflicting setting done elsewhere.

Is it also dealt with inadequately in the docs.

Like, where does /etc/default/login come in (or is there one in QNX6, I
actually haven’t checked yet)?

The login program will generally clear your environment. Any env
variables in /etc/default/login will NOT be cleared. If they have
a value specified, they will be exported with that value to the new
login session, if they are just listed, they will be passed with the
value intact. Any env variables not specified in /etc/default/login
will not be passed.

I don’t know how this interacts with phlogin.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Kevin Stallard <kevin@ffflyingrobots.com> wrote:

snip

First some basics:

  1. When you login, the following occurs (there may be more stuff, but
    this
    should be sufficient for this discussion.)
    a. The $HOME variable is set from your home dir entry in the passwd
    file (make sure this directory actually exists and that it is owned by
    you
    and at least one of the groups to which you belong).
    b. /etc/profile is run. This is a script MAKE SURE it has execute
    permissions.
    c. $HOME/.profile script is run…make sure this also has execute
    permissions (these two have been problems for me and others)
    d. A shell is started.

NOPE. Wrong order here.

login sets the $HOME environment variable.
I’m sure it does, but it uses the entry in your passwd file. So I’m
technically still correct.

Sorry, I wasn’t saying that you were wrong about where $HOME came from,
you were correct about that. You were wrong about the fact that /etc/profile
is run, then $HOME/.profile is run, THEN a shell is started.

A shell is started, that shell runs /etc/profile, runs $HOME/.profile,
etc.


It then runs a shell with argv[0][0]=’-’

try a “sin arg”, you will see something like:
563 dagibbs -sh

Under QNX 6, this is easier to read as “pidin arg”, but “sin arg” will
also give it – though the format is a bit harder to read.

check for the ENV environment variable, if it is set:
– include & evaluate the shell commands in $ENV (if it exists)

Traditionally the file ENV points to is $HOME/.rc, e.g.
/home/dagibbs/.kshrc or /home/dagibbs/.shrc
/etc/profile sets ENV. (At least under QNX 6) The following lines can be
found there:
if test -z “$ENV”; then;
ENV=~/.$(SHELL##*/}
if test -x $ENV; then
export ENV
else
unset ENV
fi
fi

I usually set ENV explicitly in my ~/.profile, so I never noticed
this change under QNX6.

So…like I said, under QNX 6.x, this $ENV will be .sh and not .kshrc or
.shrc like it was in QNX 4.2x
(If the shell you are running is ‘sh’)

But, the keypoint is that the name of the file doesn’t matter – it just
happens to be whatever file ENV points to.

Well…funny you should ask.

If you run “pterm -l” it will start your shells as a “login shell” and
you will get all the normal startup that you expect.

This is generally the easiest and cleanest way of getting a consistent
environment in your pterm shells, and the one I use.
However you neglect to see that /etc/profile and $HOME/.profile are run by
phlogin, thus setting ENV variables that can be inherited before any pterm
is run.

No, they’re not run by phlogin.

Like login, phlogin runs a shell.

Code looks kind of like:

shell = basename(pw->pw_shell);
bshell = strdup(shell);
argv[0] = bshell;
*bshell = ‘-’;
execvp(shell, argv );

(This is both modified for clarity and simplified – there is error
checking, different string manipulations, etc.)

But, again, a shell is started with argv[0][0] = ‘-’ and it will read
the /etc/profile and $HOME/.profile and, $ENV files for parsing.

Thus you don’t have to go and change any refrences to the laucning
of pterm and add a -l to the invocation.

This was my whole point. It all will work as it comes w/o invocting pterm
with the -l.

Unless, of course, you want to make some changes in environment variables
or whatever and see them without relaunching/relogging in to photon.

But, yes, I guess this wouldn’t be generally needed. Then again, I tend
to not shutdown my photon sessions in some cases for weeks.

If you don’t belive me, do the following:
Remove any pterm invoking with the -l (i.e. from the shelf)
Insure your $HOME/.profile has some crazy env variable that it sets.
I.E. export SCOOBY=12345
Logoff photon
At the graphical photon login screen…login (don’t just type ph from
your text login in other words)
Open a pterm
type: echo $SCOOBY

Sure, but it has a shell do the parsing – phlogin doesn’t. Like login,
it starts a login shell.

(Also, I generally never use phlogin, I login to a shell, and run
Photon with the ph script after logging in. This also gives me a
full set of environment variables, as I’ve been through a normal
login procedure.)

The reason I’m so insistant with this is that I thought that the -l would
necessary as well, but I want to point out that the default settings do
work and no change to them are necessary (i.e. modifying the pterm shelf
item and adding -l after you install QNX 6.x).

Sorry – yes it does work, you are correct. pterm -l does, sometimes,
by you not needing to relogin at the Photon level.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Kevin Stallard <kevin@ffflyingrobots.com> wrote:

Ahem…after reading this over…I don’t like it’s tone. Sorry if I
sound condescending, I don’t mean to.

Not a problem – my tone was a bit rough as well.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David Gibbs <dagibbs@qnx.com> wrote:

It then runs a shell with argv[0][0]=’-’

try a “sin arg”, you will see something like:
563 dagibbs -sh

Under QNX 6, this is easier to read as “pidin arg”, but “sin arg” will
also give it – though the format is a bit harder to read.

As this would suggest – when I was first responding to this,
I didn’t know whether it was being run under QNX4 or QNX6,
as nothing in the initial post identified an OS.

I know at least some versions of phlogin under QNX4 didn’t
have the processing of the /etc/profile and $HOME/.profile
being done.

Of course, the thread in the rtp newsgroup would clearly be
QNX6, but I hit this one, in comp.os.qnx, first. :slight_smile:

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.