shellcshtcsh

Print all local variables in a shell


Some context

I need to emulate the behaviour of the shell command source but in Python. Specifically sourcing tcsh scripts, but it would be nice for it to be as generic as possible.

I've seen this answer where it makes the shell run env to print all environment variables.

My question

env or printenv only prints environment variables but not local variables. However the tcsh scripts I want to source use commands like

set FOO=bar

So is there a way in tcsh (and other shells, just for completeness) to print all local variables?

Edit: Yes I know I'd have to run TCSH as a subprocess for it to parse the script, and yes python can't read into TCSH's memory which is why I want a way to print all local variables within TCSH so Python can read them.


Solution

  • For the record, I solved this a while ago thanks to @chepner's comment

    The set command with no arguments may be what you want.

    Here's the resulting python library which accomplishes reading local and environment variables set by scripts for most major shells out of the box, with some customization available to support any shell.