ITNOA
我想寫 bash 腳本,洗掉一些環境變數
我的問題是當我在命令下運行時
env | grep -i _proxy= | cut -d "=" -f1 | xargs -I {} echo {}
我看到下面的結果
HTTPS_PROXY
HTTP_PROXY
ALL_PROXY
但是當我更換echo使用unset,像下面
env | grep -i _proxy= | cut -d "=" -f1 | xargs -I {} unset {}
我看到下面的錯誤
xargs: unset: No such file or directory
我的問題是什么?如果我使用xargs不當?
uj5u.com熱心網友回復:
您在管道中運行了 xargs。因此它運行在一個單獨的行程中,它不能改變“父”shell 的環境。
此外, xargs 與命令一起作業,而不是 shell 內置函式。
你需要這樣做:
while read -r varname; do unset "$varname"; done < <(
env | grep -i _proxy= | cut -d "=" -f1
)
或者
mapfile -t varnames < <(env | grep -i _proxy= | cut -d "=" -f1)
unset "${varnames[@]}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/333143.html
