Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 555
andreas-h Avatar asked Feb 05 '13 17:02

andreas-h


People also ask

What does variable expansion mean?

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.

What is variable expansion in bash?

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 .

How do you expand a variable in SED?

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.

What is Echo variable?

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.


1 Answers

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
like image 179
that other guy Avatar answered Sep 21 '22 13:09

that other guy