3 回答

TA贡献1821条经验 获得超4个赞
您需要export该功能(带有-f选项):
$ function func()
> {
> echo "Custom env : $CUSTOM_ENV"
> }
$ export -f func
$ export CUSTOM_ENV="abc"
$ python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('func')
Custom env : abc
0
请注意,exporting 函数(和变量)会将函数和变量的副本导出到它们从中导出的 shell 的子进程。它们在父(或兄弟)进程中不可用。此外,在子流程中更改它们不会影响原始副本。
此外,导出函数是仅用于 bash 的事情,因此这仅在父 shell 和从 python 启动的 shell 都是 bash 时才有效。在 bash 不是默认值的操作系统上(例如最近发布的 Ubuntu 和 Debian),您需要明确运行 bash,否则它将无法运行。这一切使它变得相当脆弱,正如@triplee 指出的那样,这不是一个好主意。
添加回答
举报