Search code examples
bashpreprocessorsungridengine

Variable expansion in comments


Is it possible to expand variables in comments inside a bash script?

I want to write a script to feed into SGE. The qsub syntax allows me to pass additional parameters to the grid engine using lines inside the bash script which begin with #$. For example,

#$ -q all.q
#$ -S /bin/bash
#$ -V
#$ -m beas
#$ -o run_20120103.out

What I want is that the -o parameter is dynamically set to a variable, say $1. So I would naively write

#$ -o run_${1}.out

However, since the line starts with a #, bash ignores it and the variable $1 is not expanded.

Any ideas? Some bash preprocessor? Some other way?

EDIT I just chose $1 as an example. It could just as well be $FOO or $BAR.


Solution

  • Variable expansion happens in shell memory, it doesn't affect the file. Therefore, it doesn't matter what bash expands.

    Instead, you can probably generate the script to be run on the fly, with everything expanded in place:

    cat << EOF | qsub [options] -
    #$ -o run_$1.out
    cmds
    EOF