What is the scope of "exported" in Unix shell variables?

  • OK, for starters, I think you mean ps --pid and not ps ---pid.
  • You don't need to echo $$ and then type the number into ps --pid number; it's good enough to type ps --pid $$.  Unless you're talking about

    # echo $$
    42
    # su joe
    % ps --pid 42
    

    in which case you're doing the right thing.

  • What were you expecting?

    --pid pidlist

      Select by process ID.  Identical to -p and p.


    -p pidlist

      Select by PID.  This selects the processes whose process ID numbers appear in pidlist.  Identical to p and --pid.

    So, when you do ps --pid PID_of_shell, you're getting the line of ps's output for the shell process only.  You might find ps -l | grep PID_of_shell more useful; it will show any line that contains PID_of_shell anywhere, including in the PPID column.  I.e., it will show child processes of the shell.  But, of course, grep 42 will find things like 7428.

  • Your guess is right; environment variables are passed from parent to child.  As indicated above, your su shell is a child of your login shell (or other parent shell).  Note, however, that a process can change its environment; sudo is somewhat notorious for doing this, and su does it too (e.g., it changes $USER, $LOGNAME, and $HOME unless you specify --preserve-environment, and even more if you do specify --login).  Also, a process can pass its children a different environment than the one it is using; the shell does that when you say something like PAGER=cat man man_page_topic.  References: 1, 2.
  • So, no, if you set (export) an environment variable in the shell in one terminal, and then start another terminal through the window manager, it will not see the environment variable, because it is not a child (or descendent) of the shell that set it.  But, if you start a new terminal window from the shell (e.g., by xterm&), then that terminal window will inherit the shell's environment.