3 回答

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
您需要export該功能(帶有-f選項(xiàng)):
$ 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
請(qǐng)注意,exporting 函數(shù)(和變量)會(huì)將函數(shù)和變量的副本導(dǎo)出到它們從中導(dǎo)出的 shell 的子進(jìn)程。它們?cè)诟福ɑ蛐值埽┻M(jìn)程中不可用。此外,在子流程中更改它們不會(huì)影響原始副本。
此外,導(dǎo)出函數(shù)是僅用于 bash 的事情,因此這僅在父 shell 和從 python 啟動(dòng)的 shell 都是 bash 時(shí)才有效。在 bash 不是默認(rèn)值的操作系統(tǒng)上(例如最近發(fā)布的 Ubuntu 和 Debian),您需要明確運(yùn)行 bash,否則它將無(wú)法運(yùn)行。這一切使它變得相當(dāng)脆弱,正如@triplee 指出的那樣,這不是一個(gè)好主意。
添加回答
舉報(bào)