如何使用sudo命令运行带有for循环的脚本

Jak*_*žal 5 bash sudo for-loop

当我发现sudo命令和for循环问题时,我正在制作一个脚本.

我有这个测试脚本

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

正常输出应该是这样的:

Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times
Run Code Online (Sandbox Code Playgroud)

但它表明了这一点:Welcome {0..10..2} times 任何想法?

bash版本:4.3.11(1) - 发布

Nat*_*son 4

中的 shellsudo不进行大括号扩展。

$ cat s.sh 
echo $SHELL
echo $SHELLOPTS
for i in {0..10..2}
      do
        echo "Welcome $i times"
      done

$ ./s.sh
/bin/bash
braceexpand:hashall:interactive-comments
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times

$ sudo ./s.sh
/bin/bash

Welcome {0..10..2} times
Run Code Online (Sandbox Code Playgroud)

seq()按照其他人的建议使用。