Escaping properly a bash command

I am trying to execute a command which has its own variable.
Specifically, I am trying to submit jobs on a cluster using

Njobs = 10
for r in res
    submit = `bsub -R 'rusage[mem=800]' -J jobname\[1-$Njobs\] julia_script.jl $r \${LSB_JOBINDEX}`
    run(submit)
end

(Taken from DrWatson’s suggestions).

The problem is that I want the cluster to get the parameter LSB_JOBINDEX and put a serial number going from 1 to Njobs. But if I use ' escaping around $LSB_JOBINDEX then it simply passes the string $LSB_JOBINDEX.
If I do not use escaping, I get an error.

How can I solve this issue?

I figured it out:

Njobs = 10
for r in res
    bsub_cmd = "julia julia_script.jl $r \${LSB_JOBINDEX}"
    submit = `bsub -R 'rusage[mem=800]' -J jobname\[1-$Njobs\] "$bsub_cmd"`
    run(submit)
end
1 Like