Guilherme
Guilherme

Reputation: 453

gnu-parallel fails when command involves environment variables

I am trying to chain a few commands with gnu-parallel. Those commands involve environment variables defined before. For instance, consider the command below, which receives a list of *.c files, emit llvm bytecode with clang to .bc files and optimize it into .rbc files with opt.

parallel --halt now,fail=1 'clang -Xclang -disable-O0-optnone -S -c -emit-llvm {} -o {.}.bc ; opt -S -mem2reg {.}.bc -o {.}.rbc ' ::: "${source_files[@]}"

The program above works just fine, but if I change clang by an ambient variable, the command stops working.

parallel --halt now,fail=1 '$COMPILER -Xclang ... ' ::: "${source_files[@]}"

Upvotes: 1

Views: 145

Answers (1)

ams
ams

Reputation: 25569

The shell does not expand environment variables inside single quotes, and parallel does not do so either (hence the env_parallel suggestion in the comments).

The easiest solution here is to use different quotes around the variable:

parallel --halt now,fail=1 "$COMPILER"' -Xclang ... ' ::: "${source_files[@]}"

I've left the single quotes around the rest of the command, just in case, but you may not need to. Quotes placed back-to-back like that are automatically concatenated into one argument by the shell.

Upvotes: 2

Related Questions