python: can you update the current shell from python?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
python: can you update the current shell from python?
Can I update the current shell with a python script?
The easy example would be an environment variable. Can I set an environment variable in a python script & make it stick after the python script exits?
What I'm actually trying to do is update the "complete" alias with a python script. For instance, this line does it when run on the command line:
complete foo 'p/1/(yabba dabba dooo)/'
... this will make it so that when you type "foo <tab>" you get the possible completions of "yabba" "dabba" or "dooo". (at least, that's how you do it in tcsh, haven't checked bash)
If I put that in a file, it works as long as I source the file as opposed to setting its exec bit & running it, which will not work.
I can run scripts from within python, but I'm not sure how to make python's changes stick in the current shell.
yes... python is can operate at the same level as any other terminal process, here's a very simple example of executing system commands (no capture of results other than exit code):
Code:
import os
iret = os.system("ls -al")
if iret != 0:
print "Error - child terminated with signal ", iret
yes... python is can operate at the same level as any other terminal process, here's a very simple example of executing system commands (no capture of results other than exit code):
Code:
import os
iret = os.system("ls -al")
if iret != 0:
print "Error - child terminated with signal ", iret
yes, that runs a system command, but it does nothing to my current shell.
for example:
Code:
os.system("setenv FOO bar")
will set an env var (in tcsh) for the time that python is running, but as soon as the python script terminates, so does that env var. I'm looking for a way to keep that env var around, or, more specifically, to keep a complete alias around.
Can I update the current shell with a python script?
I can run scripts from within python, but I'm not sure how to make python's changes stick in the current shell.
Any ideas?
Sure arrange to source the output of a Python script then the Python script can output the text to set an environment variable (or three), and that will be sourced by the outer shell so changing the environment for subsequent commands.
well i'm having trouble with lot! I can't seem to do 'export LFS=/mnt/lfs', use declare or setenv (setenv is giving errors), even straight from shell? It's 6.10am an i'm off to bed, i'll look again tomorrow>>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.