linux shell脚本中for循环的语法

mka*_*kab 18 linux bash shell for-loop dash-shell

我在实现for循环时遇到问题.我执行脚本时遇到此错误

test1.sh:2:语法错误:循环变量错误

我不明白这个错误.

这是我的剧本

#!/bin/bash
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times..."
done
Run Code Online (Sandbox Code Playgroud)

任何人都可以在ubuntu中告诉我for循环的语法(在ubuntu中它链接到破折号shell)shell吗?

Mic*_*ker 36

你可能用它来运行它sh,而不是bash.尝试bash test1.sh,或者./test1.sh如果它是可执行的,但不是sh test1.sh.


Ken*_*oom 5

标准POSIX shell仅接受语法 for varname in list

类似C的for循环语法for (( expr1; expr2; expr3 ))是一种基础.

您可以使用标准POSIX shell中获得类似的行为 for c in $(seq 1 5)