Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting environment variables with GNU Parallel

I would like to inherit environment variables in GNU Parallel. I have several 'scripts' (really just lists of commands, designed for use with GNU Parallel) with hundreds of lines each that all call different external programs. However, these external program (out of my control) requires that several environment variables be set before they will even run.

Setting/exporting them locally doesn't seem to help, and I don't see any way to add this information to a profile.

The documentation doesn't seem to have anything this, and similar SO pages suggest wrapping the command in a script. However, this seems like an inelegant solution. Is there a way to export the current environment, or perhaps specify the required variables in a script?

Thanks!

like image 621
Worbis Avatar asked Aug 23 '12 21:08

Worbis


1 Answers

This works for me:

FOO="My  brother's  12\"  records"
export FOO
parallel echo 'FOO is "$FOO" Process id $$ Argument' ::: 1 2 3

To make it work for remote connections (through ssh) you need to quote the variable for shell expansion. parallel --shellquote can help you do that:

parallel -S server export FOO=$(parallel --shellquote ::: "$FOO")\;echo 'FOO is "$FOO" Process id $$ Argument' ::: 1 2 3

If that does not solve your issue, please consider showing an example that does not work.

-- Edit --

Look at --env introduced in version 20121022

-- Edit --

Look at env_parallel introduced in 20160322.

like image 131
Ole Tange Avatar answered Oct 10 '22 22:10

Ole Tange