Shell:`for`的未定义行为

Dee*_*eyi 2 bash shell sh

我有一个看起来像这样的脚本:

test.sh

!/bin/bash                                                                  
for i in {0..10}                                                          
  do                                                                         
    echo "Welcome $i times"
  done
Run Code Online (Sandbox Code Playgroud)

如果我像sh test.shWelcome {0..10} times只得到一次那样运行它

但是,如果我按照以下方式运行它bash test.sh:

Welcome 0 times
Welcome 1 times
 .... and so on
Run Code Online (Sandbox Code Playgroud)

如果我在redhat终端上运行相同的脚本.我收到一个错误:

root@UPLC-PPM-DEV-01 TEST]# sh test.sh 
'est.sh: line 3: syntax error near unexpected token `do
'est.sh: line 3: `  do
Run Code Online (Sandbox Code Playgroud)

这有什么不对?

Kar*_*ath 6

这与此无关for.

{0..10}语法只能由bash的支持.

在简单的sh你可以使用

seq 0 10
Run Code Online (Sandbox Code Playgroud)

(如果您的平台支持它),或

i=`expr $i + 1`
Run Code Online (Sandbox Code Playgroud)

创建一个计数器并使用while.