Bash: xargs variable passing

How is a global script variable passed to the xargs command? I tried it like this:

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

Output:

 passed=hallo test= 

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

I am using bash .

+6
source share
3 answers

Take the $TEST variable from quotes:

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

Added as answer suggested by @chepner.

export variable TEST :

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

With GNU Parallel, you can avoid some quotes:

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

GNU Parallel exists as a package for most distributions, and it is recommended that you use the standard package manager to install it. But if there is no package for your system, it takes literally 10 seconds to install GNU Parallel:

 wget pi.dk/3 -qO - | sh -x 

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

-5
source

All Articles