Note that there are some explanatory texts on larger screens.

plurals
  1. POBASH Arithmetic Issues
    primarykey
    data
    text
    <p>I'm working in BASH and I'm having an idiot moment right now. I've got a project I'm working on that I'm going to need to use some very basic arithmetic expressions and I just realized that a lot of my problems with it are because my variables are not updating. So I threw together a basic algorithm that increments a variable by another variable with a while loop until a certain number is reached.</p> <pre><code>counter=1 counter2=0 while [[ counter2 &lt; 10 ]]; do counter2=$(($counter2+$counter)) echo $counter echo $counter2 done </code></pre> <p>I run the script. Does nothing. I set the <code>&lt;</code> to <code>&gt;</code> just for kicks and an infinite loop occurs with a repeated output of:</p> <pre><code>1 0 1 0 </code></pre> <p>Forever and ever until I stop it. So it's obvious the variables are not changing. Why? I feel like such an idiot because it must be something stupid I'm overlooking. And why, when I have <code>&lt;</code>, it also isn't an infinite loop? Why doesn't it print anything at all for that matter? If <code>counter2</code> is always less than 10, why doesn't it just keep going on forever?</p> <p>Thanks folks in advance.</p> <p>EDIT: Well, I realize why it wasn't outputting anything for when the check is <code>&lt;</code>... I should have been using <code>$counter2</code> instead of just <code>counter2</code> to get the actual value of <code>counter2</code>. But now it just outputs:</p> <pre><code>1 2 </code></pre> <p>And that's it... I feel like such a derp.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload