Bash 4.2 / 4.3: different behavior in a C-style loop

bash

4.2 shows the intended correct behavior in a C loop:

me@server:/some/dir# TIMES=30; for (( n=0; n<$(shuf -i ${TIMES}-$(expr ${TIMES} + 20) -n 1); n++ )); do echo $n; done
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
me@server:/some/dir# bash --version
GNU bash, Version 4.2.25(1)-release (x86_64-pc-linux-gnu)
(...)
me@server:/some/dir#

      

Same in bash

4.3 throws an error:

me@server:/some/dir# TIMES=30; for (( n=0; n<$(shuf -i ${TIMES}-$(expr ${TIMES} + 20) -n 1); n++ )); do echo $n; done
-bash: syntax error near unexpected token `newline'
me@server:/some/dir# bash --version
GNU bash, Version 4.3.30(1)-release (x86_64-pc-linux-gnu)
(...)

      

However, part of finding a random number between ${TIMES}

and ${TIMES}+20

works:

me@server:/some/dir# shuf -i 20-50 -n 1
26
me@server:/some/dir#

      

It also inserts a digit directly instead of $()

-subshell'ing:

me@server:/some/dir# TIMES=30; for (( n=0; n<26; n++ )); do echo $n; done
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
me@server:/some/dir#

      

What's going on here? Any ideas why the subshell is not executing correctly in bash

4.3?

+3


source to share


1 answer


If you replace $(expr

with $((

, it starts working:



TIMES=30
for (( n=0; n < $(shuf -i $TIMES-$((TIMES + 20)) -n 1); n++ )) ; do
    echo $n
done

      

+5


source







All Articles