为什么我的 Linux bash 中这个简单的 FOR LOOP 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21370059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:22:58  来源:igfitidea点击:

why is this simple FOR LOOP in my Linux bash not working?

linuxbashshellunix

提问by John1024

I'm trying to do a simple for loop in a UNIX script (bash).

我正在尝试在 UNIX 脚本 (bash) 中执行一个简单的 for 循环。

Here's my script:

这是我的脚本:

for i in {1..3}
do
   echo "Welcome $i times"
done

I was expecting this for output ...

我期待这个输出......

Welcome 1 times
Welcome 2 times
Welcome 3 times

... but I get this ...

......但我明白了......

Welcome {1..3} times

What am I doing wrong?

我究竟做错了什么?

回答by John1024

You didn't mention how you were executing your script and that can make a difference. Suppose we have a script:

您没有提到您是如何执行脚本的,这可能会有所作为。假设我们有一个脚本:

$ cat welcome.sh
for i in {1..3}
do
   echo "Welcome $i times"
done

Observe the following three invocations of welcome.sh from a bash shell:

从 bash shell 观察以下三个对welcome.sh 的调用:

$ ps -p $$
  PID TTY          TIME CMD
11509 pts/25   00:00:00 bash
$ source welcome.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
$ bash welcome.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
$ sh welcome.sh
Welcome {1..3} times

The last one fails because, on my system, shdefaults to dash, not bash. This is true, for example, for any modern Debian/Ubuntu-derived system.

最后一个失败,因为在我的系统上,sh默认为dash,而不是bash。例如,对于任何现代 Debian/Ubuntu 派生系统来说都是如此。

回答by bishop

Moving my comment to formal answer:

将我的评论移至正式答案:

set -o braceexpand

That enables {x..y}style (amongst other kinds of) expansion. If you want it permanently, add it to your .bashrc. If you want it temporarily, you can "bracket" your code:

这使得{x..y}样式(以及其他种类)扩展成为可能。如果您想要永久使用它,请将其添加到您的.bashrc. 如果你暂时需要它,你可以“括号”你的代码:

set -o braceexpand  # enable brace expansion
echo {1..3}         # or whatever your command is
set +o braceexpand  # disable it

Frankly I think the code overhead of that on/off approach isn't worth it, and I always add brace expansion to my .bashrc.

坦率地说,我认为这种开/关方法的代码开销不值得,而且我总是在我的.bashrc.

Finally, here's an excellent discussion of brace expansion ins and outs.

最后,这里有一个关于支撑扩展的来龙去脉的精彩讨论。

回答by David W.

Several things to try:

要尝试的几件事:

  • Run your script with bash as a prefix. bash myscript.shinstead of simply myscript.sh. This will guarantee you're running under BASH.
  • Run set -oand see if braceexpansionis set to on. If not, run set -o braceexpandand see if that fixes your problem.
  • 使用 bash 作为前缀运行您的脚本。bash myscript.sh而不是简单地myscript.sh。这将保证您在 BASH 下运行。
  • 运行set -o并查看braceexpansion是否设置为on。如果没有,请运行set -o braceexpand并查看是否可以解决您的问题。

You can test for whether braceexpandis on or off with the -otest.

您可以测试是否braceexpand开启或关闭与-o测试

if [[ -o braceexpand ]]
then
    echo "Brace expand is on"
else
    echo "It is off"
fi

You can use this to test the state of braceexpand, so you can return it to its previous state.

您可以使用它来测试braceexpand的状态,以便您可以将其返回到之前的状态。