我有一個文件,如下所示。xargs與導出不起作用
HOST=localhost
PORT=8080
我想將上述內容導出到環境中。我正在執行以下命令將文件中的變量導出到環境中。
cat <FileName> | xargs export
試圖運行上面的命令時,我得到了以下異常。
xargs: export: No such file or directory
我有一個文件,如下所示。xargs與導出不起作用
HOST=localhost
PORT=8080
我想將上述內容導出到環境中。我正在執行以下命令將文件中的變量導出到環境中。
cat <FileName> | xargs export
試圖運行上面的命令時,我得到了以下異常。
xargs: export: No such file or directory
爲什麼使用xargs
?當你可以source
在當前shell文件
您可以使用內置set
與export
而是採用了非shell內建像xargs
一起。只要做到以下
set -o allexport
. ./file_containing_variables
set +o allexport
allexport
標誌的使用與您設置(-o)/復位(+ O),您可以直接在命令行輸出變量。第二行是POSIX
source
(點後跟變量名)命令以反映所有變量到當前的外殼以及allexport
標誌內,它將永久可用。
請參閱GNU set
built-in頁面瞭解更多詳情。
使用上的文件
while IFS= read -r line; do
export "$line"
done <file_containing_variables
export
再方向輸入在bash
另一種方式是一個內置的功能,並且需要xargs
可執行作爲參數,而不是一個函數。另外,將export
包裝到某些腳本或子shell中是沒有意義的,在這種情況下,xargs
不會失敗,但是您的變量無論如何都不會導出。 所以最好選擇另一種方法,如前面的評論中所示,不要使用xargs
。
[將所有變量從一個shellcript傳遞給另一個?](https://stackoverflow.com/questions/9772036/pass-all-variables-from-one-shellscript-to-another) –
爲什麼你不接受答案?它有用嗎?不行?除非你知道,否則我們無法幫助你 – Inian