Because {1..$num_in}
did not expanded to sequences of numbers, it's only expanded to literal string like {1..1}
, {1..2}
and so on. So, your script performed arithmetic expansion, it saw an invalid number, and print error message.
When you use your shebang as #!/bin/sh
, it depends on system to use what shell /bin/sh
linked to for running your script. Thus, the error message can be varied depending on the shells.
With dash
:
$ dash test.sh
aaaa
test.sh: 74: test.sh: Illegal number: {1..3}
With bash
:
$ bash test.sh
aaaa
test.sh: line 74: {1..3}: syntax error: operand expected (error token is "{1..3}")
NOT equal to 6
total= 0
With pdksh
and mksh
:
$ pdksh test.sh
aaaa
test.sh[77]: {1..3}: unexpected '{'
NOT equal to 6
total= 0
With yash
:
$ yash test.sh
aaaa
yash: arithmetic: `{1..3}' is not a valid number
posh
even through a segmentation fault:
$ posh test.sh
aaaa
test.sh:77: {1..3}: unexpected `{'
Segmentation fault
The script will work with zsh
and ksh93
:
$ zsh test.sh
aaaa
aaaa
aaaa
equa to 6 6
total= 6
z
beforesh
in your shebang, that is why it doesn't work. – casey Jan 24 '15 at 02:31