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
.
Variable expansion is the term used for the ability to access and manipulate values of variables and parameters. Basic expansion is done by preceding the variable or parameter name with the $ character. This provides access to the value.
Bash uses the value formed by expanding the rest of parameter as the new parameter ; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter . This is known as indirect expansion .
Just use double quotes instead of single quotes. You'll also need to use {} to delimit the number_line variable correctly and escape the \ , too.
Variables are an essential feature of bash programming in which we assign a label or name to refer to other quantities: such as an arithmetic command or a value. They are used to make the machine programs more readable for humans. Using the echo command you can display the output of a variable or line of text.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With