bash: xargs passing variable bash: xargs passing variable linux linux

bash: xargs passing variable


Take variable $TEST out from the quotes:

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


Added as an answer as@chepner suggested.

export the variable TEST:

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


With GNU Parallel you can avoid some of the quoting:

TEST=hallo2echo "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.

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