C Program to execute programs in same console

Alex Zbyslaw xfb52 at dial.pipex.com
Wed Apr 5 15:06:48 UTC 2006


Jonathan Herriott wrote:

>What I am trying to do is execute the command in the calling shell. 
>So, if I were to execute my program, which changes the directory, it
>would do the following:
>
>>pwd
>>    
>>
>/usr/home/username/
>  
>
>>./myprog ..
>>pwd
>>    
>>
>/usr/home/
>
>That's basically what I'm looking for.  Being able to modify the
>calling shell with a program.
>  
>
Write a shell script.  If using sh/bash? run it as ". shellscript" or if 
csh "source shellscript".  You can then make aliases (at least in csh) 
like so:
    alias foo "source shellscript"

Then when you type "foo", any cd inside the shellscript will affect your 
current shell.

e.g.

% cat foo.sh
cd ..
% alias foo "source foo.sh"
% pwd
/tmp
% foo
% pwd
/
%

This is a rubbish alias since it only works if you are in the same 
directory as the shellscript, but I'm sure you can do better :-)

To my mind you are still explaining the solution you have come up with 
for some problem, but have not actually explained the original problem.  
Your "solution" is generally impossible since one process (your C 
program) cannot arbitrarily affect the running environment (the current 
directory) of another process (your shell).

--Alex



More information about the freebsd-questions mailing list