bash
4.2 show the assumed correct behavior in a C-style for 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#
The same under 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)
(...)
Yet the part to find a random number between ${TIMES}
and ${TIMES}+20
works:
me@server:/some/dir# shuf -i 20-50 -n 1
26
me@server:/some/dir#
So does inserting the numeral directly instead of $()
-subshell'ing it:
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 executed correctly under bash
4.3?
If you replace
$(expr
with$((
, it starts to work: