GNU Parallel - global variables and function

4

I have this script:

GLOBAL_VAR="some global value"

function test
{
  echo $1
  echo ${GLOBAL_VAR}
}

export -f test  
parallel --jobs 5 --linebuffer test ::: "${files[@]}"

How can I have $GLOBAL_VAR visible from parallel?

Martin Perry

Posted 2017-06-23T13:59:38.460

Reputation: 143

Answers

6

Shell variables are not visible to child processes unless they are exported so you'll need to say

export GLOBAL_VAR

somewhere before the child process is launched; in modern shells the export can be combined with the assignment

export GLOBAL_VAR="some global value"

thrig

Posted 2017-06-23T13:59:38.460

Reputation: 686

0

"Shell variables are not visible to child processes unless they are exported" - and even so they are still not visible if you run a command remotely. That is unless you use env_parallel which is a frontend for GNU Parallel that copies the environment:

   . `which env_parallel.bash`

   aliases
             alias myecho='echo aliases'
             env_parallel myecho ::: work
             env_parallel -S server myecho ::: work
             env_parallel --env myecho myecho ::: work
             env_parallel --env myecho -S server myecho ::: work

   functions
             myfunc() { echo functions $*; }
             env_parallel myfunc ::: work
             env_parallel -S server myfunc ::: work
             env_parallel --env myfunc myfunc ::: work
             env_parallel --env myfunc -S server myfunc ::: work

   variables
             myvar=variables
             env_parallel echo '$myvar' ::: work
             env_parallel -S server echo '$myvar' ::: work
             env_parallel --env myvar echo '$myvar' ::: work
             env_parallel --env myvar -S server echo '$myvar' ::: work

   arrays
             myarray=(arrays work, too)
             env_parallel -k echo '${myarray[{}]}' ::: 0 1 2
             env_parallel -k -S server echo '${myarray[{}]}' ::: 0 1 2
             env_parallel -k --env myarray echo '${myarray[{}]}' ::: 0 1 2
             env_parallel -k --env myarray -S server echo '${myarray[{}]}' ::: 0 1 2

Ole Tange

Posted 2017-06-23T13:59:38.460

Reputation: 3 034