Linux bash:xargs 传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15430877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:27:57  来源:igfitidea点击:

bash: xargs passing variable

linuxbashvariablesxargs

提问by arminb

How can a global script variable be passed to the command of xargs? I tried it this way:

如何将全局脚本变量传递给 xargs 的命令?我是这样试的:

TEST=hallo2
echo "hallo" | xargs sh -c 'echo passed= test=$TEST' sh

Output:

输出:

passed=hallo test=

I know I could use the {}token but I need to do it this way!

我知道我可以使用{}令牌,但我需要这样做!

I'm using bash.

我正在使用bash.

采纳答案by Ander2

Take variable $TESTout from the quotes:

$TEST从引号中取出变量:

 TEST=hallo2
 echo "hallo" | xargs sh -c 'echo passed= test='$TEST sh

回答by Carlos Campderrós

Added as an answer as@chepner suggested.

作为@chepner 建议的答案添加。

exportthe variable TEST:

export变量TEST

export TEST=hallo2
echo "hallo" | xargs sh -c 'echo passed= test="$TEST"' sh

回答by Ole Tange

With GNU Parallel you can avoid some of the quoting:

使用 GNU Parallel,您可以避免一些引用:

TEST=hallo2
echo "hallo" | parallel echo passed={} test=$TEST

GNU Parallel exists as package for most distributions and it is recommended to use the normal package manager for installation. But if there is no package for your system, it still takes literally 10 seconds to install GNU Parallel.

GNU Parallel 作为大多数发行版的包存在,建议使用普通的包管理器进行安装。但是如果你的系统没有安装包,安装 GNU Parallel 仍然需要 10 秒钟。

Watch the intro videos to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

观看介绍视频以了解更多信息:https: //www.youtube.com/playlist?list=PL284C9FF2488BC6D1