Search code examples
variable-assignmentgnu-parallel

How to assign variables in gnu parallel?


I want to assign a variable in the command that I pass to parallel:

parallel "my_variable={}_33; echo $my_variable" ::: a b c

The output should be:

# a_33
# b_33
# c_33

Of course, this is just a toy example. In the real example I want to do other things with that variable.


Solution

  • You are so close to solving it yourself. You just forgot that there is a difference between " and ':

    parallel 'my_variable={}_33; echo $my_variable' ::: a b c
    

    If you are doing advanced stuff, remember that you can use bash functions:

    doit() {
       echo Doing it for $1
       sleep 2
       echo Done with $1
    }
    export -f doit
    parallel doit ::: 1 2 3