我正在使用Ruby 2,我为一些环境变量提供资源并将其放入〜/ .profile,然后在ruby脚本中重新加载.profile,如下所示:
#!/usr/bin/ruby env
`source ~/.profile`
但是我没有找到源命令。如何在Ruby脚本中重新加载.profile?
我也尝试过这个:
#!/usr/bin/ruby env
`/bin/bash -c 'source ~/.profile'`
但是没有成功。
最佳答案
简而言之-您不能。
源环境变量仅在子流程中有效,不会影响父流程。
唯一的方法是使用bash -c "source ~/.profile; env"
返回环境变量,然后解析并在脚本中进行设置。