16

In bash script, I want to iterate over a list of values that I want to pass as parameters to a script in python. It turns out that $d and $minFreq aren't floats when passed to the python script. Why does this happen?

for d in {0.01, 0.05, 0.1} do for i in {1..3} do someString=`python scrpt1.py -f myfile --delta $d --counter $i| tail -1` for minFreq in {0.01, 0.02} do for bValue in {10..12} do python testNEW.py $someString -d $bValue $minFreq done done done done 
1
  • Lose the spaces around the equals sign in the someString = $(python ...) line (with back-ticks replaced by $(...) because getting back-ticks into a comment as back-ticks is tricky, at best. Commented Aug 10, 2012 at 13:34

1 Answer 1

30

Either remove the spaces

for d in {0.01,0.05,0.1} 

or don't use the {} expansion (it's not necessary here):

for d in 0.01 0.05 0.1 

The same applies to the minFreq loop.


As written,

for d in {0.01, 0.05, 0.1} 

the variable d is assigned the literal string values {0.01,, 0.05,, and 0.1}.

Sign up to request clarification or add additional context in comments.

2 Comments

While the spaces in the expansion are unnecessary, the Python process should not see them at all, I think. I'd expect the shell to remove the spaces when constructing the argument list since the shell variables are not quoted.
The spaces prevent brace expansion from taking place, so the braces and commas are not syntax, but included in the strings passed to python.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.