bash: xargs passing variable

arminb picture arminb · Mar 15, 2013 · Viewed 28.1k times · Source

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

TEST=hallo2
echo "hallo" | xargs sh -c 'echo passed=$1 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.

Answer

Ander2 picture Ander2 · Mar 15, 2013

Take variable $TEST out from the quotes:

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