home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / shell / 3543 < prev    next >
Encoding:
Text File  |  1992-08-20  |  3.5 KB  |  93 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!convex!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: How can process cd to a dir and stay there when exits
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Aug20.131345.3957@news.eng.convex.com>
  8. Date: Thu, 20 Aug 1992 13:13:45 GMT
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. References: <BtA88K.MC1@encore.com>
  11. Nntp-Posting-Host: pixel.convex.com
  12. Organization: Convex Computer Corporation, Colorado Springs, CO
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 75
  17.  
  18. From the keyboard of mpalmer@encore.com (Mike Palmer):
  19. :Can anyone suggest a way for a process to cd to a dir & stay there when the \
  20. :process exits.
  21. :
  22. :I have a ksh solution using an alias, a script & the program, but I'd like
  23. :to do it just with a program, or at least a script & program than will work
  24. :under csh & ksh.
  25.  
  26. FAQqed again.
  27.  
  28. --tom
  29.  
  30.  
  31. 14) How do I {set an environment variable, change directory} inside a
  32.       program or shell script and have that change affect my
  33.       current shell?
  34.  
  35.     In general, you can't, at least not without making special
  36.     arrangements.  When a child process is created, it inherits a copy
  37.     of its parent's variables (and current directory).  The child can
  38.     change these values all it wants but the changes won't affect the
  39.     parent shell, since the child is changing a copy of the 
  40.     original data.
  41.  
  42.     Some special arrangements are possible.  Your child process could
  43.     write out the changed variables, if the parent was prepared to read
  44.     the output and interpret it as commands to set its own variables.
  45.  
  46.     Also, shells can arrange to run other shell scripts in the context
  47.     of the current shell, rather than in a child process, so that
  48.     changes will affect the original shell.
  49.  
  50.     For instance, if you have a C shell script named "myscript":
  51.  
  52.         cd /very/long/path
  53.         setenv PATH /something:/something-else
  54.  
  55.     or the equivalent Bourne or Korn shell script
  56.  
  57.         cd /very/long/path
  58.         PATH=/something:/something-else export PATH
  59.  
  60.     and try to run "myscript" from your shell, your shell will fork and run
  61.     the shell script in a subprocess.  The subprocess is also
  62.     running the shell; when it sees the "cd" command it changes
  63.     *its* current directory, and when it sees the "setenv" command
  64.     it changes *its* environment, but neither has any effect on the current
  65.     directory of the shell at which you're typing (your login shell,
  66.     let's say).
  67.  
  68.     In order to get your login shell to execute the script (without forking)
  69.     you have to use the "." command (for the Bourne or Korn shells)
  70.     or the "source" command (for the C shell).  I.e. you type
  71.  
  72.         . myscript
  73.     
  74.     to the Bourne or Korn shells, or
  75.  
  76.         source myscript
  77.  
  78.     to the C shell.
  79.  
  80.     If all you are trying to do is change directory or set an
  81.     environment variable, it will probably be simpler to use a
  82.     C shell alias or Bourne/Korn shell function.  See the "how do
  83.     I get the current directory into my prompt" section
  84.     of this article for some examples.
  85.  
  86.  
  87.  
  88. -- 
  89.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  90. Unix is like a toll road on which you have to stop every 50 feet to
  91. pay another nickel.  But hey!  You only feel 5 cents poorer each time.
  92.     --Larry Wall in <1992Aug13.192357.15731@netlabs.com>
  93.