Why is a sleep required? ksh/bash
<p>I have the following functions</p>

<pre><code>get_previous_term() {
@var term=$1
@var centry=${term:0:2}
@var year=${term:2:2}
@var semester=${term:4:2}

case $semester in
    10)
    semester=$((semester+30))
    year=$((year-1))
    ;;
    # years stay the same
    20)
    semester=$((semester-10))
    ;;
    30)
    semester=$((semester-10))
    ;;
    40)
    semester=$((semester-10))
    ;;
    *)
    error_exit "Unknown Term $term"
    ;;
esac;
@var prev_term=$((centry))$((year))$((semester))
echo $prev_term
}

get_n_terms_ago() {
@var term=$1
@var counter=$2
@var tmp=$term
@var i=0
while [ $i -lt $counter ]; do
    tmp=$(get_previous_term $term)
    term=$tmp
    i=$((i+1))
done;
echo $term
}
</code></pre>

<p>(The @var is just typesetting (part of the oo-bashframework))</p>

<p>Now the question is this. Say I want to get 3 of the previous terms from a term that start with 201610 as such.</p>

<pre><code>   current_term="201610"
   previous_term=$(get_previous_term $current_term)
   previous_2_term=$(get_n_terms_ago $current_term 2)
   previous_3_term=$(get_n_terms_ago $current_term 3)
</code></pre>

<p>If I set one variable by itself it works fine. If I do them sequentially I will get a command not found (from shell). I have been able to get it to reliably work by using sleep's between the variable assignments. Example</p>

<pre><code>   current_term="201610"
   previous_term=$(get_previous_term $current_term)
   sleep 1;
   previous_2_term=$(get_n_terms_ago $current_term 2)
   sleep 2;
   previous_3_term=$(get_n_terms_ago $current_term 3)
   sleep 1;
</code></pre>

<p>I'm not sure what is happening? Does $() spawn another shell? Shouldn't the shell wait for the previous command to be completed? (Also if I omit the first sleep (even if i use get_n_terms_ago with n of 1) I will not have issues. Seems to happen on setting the 2/3rd variables. </p>