When I'm using a shell, it is (AFAIK) impossible for a shell script which contains a source somefile line to affect the parent shell (unless the script was itself sourced, of course).

I want to know if a non-shell executable (written in C, for instance) can effectively source a file and have everything be available to the shell as if I had typed source somefile on the command line.

$ type source
source is a shell builtin

That’s how it’s possible (that made more sense with your original, longer question).

No, you can’t replicate the behaviour of source in an external program (short of exploiting input buffering tricks). It’s the same as with cd: only the shell can modify its own internal state, so only shell builtins can modify the shell’s internal state.

  • Can you comment on the input buffering tricks? – iconoclast Mar 28 '17 at 18:52
  • It’s disabled by default in most modern environments, but see CVE-2008-2383 for the general idea: in many terminals, you can change the title to anything you want, then ask the terminal to tell you what its title is, and exit — so the terminal tells the shell what its title is. If you’ve ever catted a binary by mistake, you might have seen your shell process rubbish commands afterwards, that’s what’s happening there. – Stephen Kitt Mar 30 '17 at 8:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.