如何使用lua设置bash环境变量 [英] How to set bash environment variables using lua
问题描述
我尝试使用
- os.execute(export MY_VAR = 10)
- os.execute("export MY_VAR=10")
- io.popen("export MY_VAR=10")
<
我尝试在执行lua脚本后使用echo $ MY_VAR从shell中读取MY_VAR变量,但我看不到MY_VAR设置为10。
我们如何使用lua脚本设置环境变量?
您的问题不是lua问题。你的问题是误解过程环境如何工作。
每次运行 os.execute
或 io.popen
您正在使用新环境执行新流程。
所以当你可能会正确地在这个进程环境中设置 MY_VAR
(并且会影响作为进程的的子进程运行的任何进程)如果您想影响 lua $>,那么这个过程不会被任何其他进程看到。
c $ c>进程的环境(这将反过来影响由lua运行的进程的环境),那么你需要绑定到 setenv
系统函数(这个lua本身不提供,因为它不通过lua用于其包含的东西的清洁 C测试。
I am new to lua script features. I tried using,
from lua script.
I try reading MY_VAR variable from shell using echo $MY_VAR after lua script is executed but I do not see MY_VAR getting set to 10.
How do we set the environment variable using lua script?
Your problem isn't a lua problem. Your problem is misunderstanding how process environments work.
Every time you run os.execute
or io.popen
you are running a new process with new environment.
So while you may be correctly setting MY_VAR
in that processes environment (and it would affect any processes run as children processes of that process) it doesn't survive beyond the death of the launched process and so cannot be seen by any other processes.
If you want to affect the lua
process's environment (which would then, in turn, affect the environment's of processes run by lua) then you need a binding to the setenv
system function (which lua itself doesn't provide as it doesn't pass the clean C test that lua uses for things it includes).
这篇关于如何使用lua设置bash环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!