5

Suppose I have foo.sh that calls bar.sh using parallel:

# foo.sh

#! /bin/bash

parallel -N 3 bar.sh ::: $(seq 10)

My bar.sh works like this: if there is an environment variable (e.g. DEBUG=1) set, then it will output lots of debug info.

Ideally I want to simply execute my foo.sh like such:

$ DEBUG=1 foo.sh

Normally, foo.sh has $DEBUG value, as well as bar.sh sees it. But now with me using GNU parallel to call bar.sh, which is a local program, my bar.sh no longer has the DEBUG value set.

I read that --env only works if I had remote execution -S set, and from me trying it does not seem to work for me.

Is there a way to get my parallel'ed bar.sh to simply "inherit" the environment settings of my foo.sh? I really don't want to spell out each and every environment variable and their values when calling bar.sh in parallel.

TIA

Stephen Chu
  • 343
  • 2
  • 15

2 Answers2

1

You are looking for env_parallel which does exactly this.

Put this in $HOME/.bashrc:

  . `which env_parallel.bash`

E.g. by doing:

  echo '. `which env_parallel.bash`' >> $HOME/.bashrc

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

env_parallel is part of GNU Parallel 20160722. It is beta quality, so please report bugs if you find any.

If your know your UNIX you will know that you cannot use aliases, non-exported functions, non-exported variables, and non-exported arrays in shells started from the current shell (e.g. in bash -c); and especially not if the shell is remote (e.g. ssh server myalias). With env_parallel this common knowledge has to be revised into: you cannot do it without cheating.

Ole Tange
  • 31,768
  • 5
  • 86
  • 104
  • I ran into an issue when trying to install the latest version of `parallel`: `The signature on parallel-20160722.tar.bz2 is wrong.` But my `(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash` was trying to download `2016-08-08`. – Stephen Chu Aug 08 '16 at 16:50
  • Follow instructions for personal installation in http://git.savannah.gnu.org/cgit/parallel.git/tree/README – Ole Tange Aug 08 '16 at 18:16
0

In order to copy the entire environment, use _ as the variable exported by --env:

parallel --env _ -N 3 bar.sh ::: $(seq 10)
chepner
  • 497,756
  • 71
  • 530
  • 681