如何使用lua设置bash环境变量 [英] How to set bash environment variables using lua

查看:566
本文介绍了如何使用lua设置bash环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的lua脚本功能。
我尝试使用


  1. os.execute(export MY_VAR = 10)

  2. <



    我尝试在执行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,

    1. os.execute("export MY_VAR=10")
    2. io.popen("export MY_VAR=10")

    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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆