5

Is there a way to make the exec system call work like a function call that returns instead of totally replacing the current process image? My motivation is trying to integrate two shells with incompatible languages. The idea is that shell A would be able to execute scripts written in the shell B language and still keep any changes to the environment variables, working directory, etc that the shell B script makes.

So far all the solutions I could think of have some big limitations.

The first solution is to have shell A exec into shell B and then have shell B exec back into shell A, like I do in this question I asked earlier. The problem is that this throws away any state from the initial shell A process, such as the position in the currently running script, local variables, aliases, etc.

The second solution is to have shell A run shell B as a traditional subprocess using fork-exec and have shell B serialize its environment just before it exits and send it back to the shell A process. Two upsides of this are that it preserves the state of the parent process and that we can also serialize application-level data such as functions and aliases if we want to. The problem is that there is a a lot of stuff that a subprocess inherits from the parent and its hard to make sure that we didn't forget to serialize one of those.

  • See there for an attempt at your second solution in bash (in that case, that was to switch user as opposed to switching shell) – Stéphane Chazelas May 19 '15 at 20:58
  • 1
    A 3rd solution would be to use a shell that can emulate other shells like zsh (it can't emulate fish but it has many of the features of fish and many more, and can interpret most ksh/bash scripts when in ksh emulation) – Stéphane Chazelas May 19 '15 at 20:59
  • When you declare a function in bash, should it be automatically translated to an equivalent fish function and vis-versa? – Stéphane Chazelas May 19 '15 at 21:01
  • haha, you are not going to make me change my shell so easily and this question is more about wondering whats possible in Unix anyway :) As for point #3, its OK if you throw away functions defined in the shell B script since I'm kind of assuming that A and B can be general executables. – hugomg May 19 '15 at 21:19
  • Apparently it's possible to change a parent process's environment: stackoverflow.com/questions/205064/… But that seems terribly hacky. – Bruce Ediger May 19 '15 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.