1.系统ubuntu 12.04、文件权限777
2.代码如下:
#!/bin/bash
for i in {1..10}
do
echo $i
done
3.使用sh test.sh执行结果为
{1..10}
4.使用./test.sh执行结果为
1
2
3
4
5
6
7
8
9
10
请问为什么步骤3和4的结果不一样呢,我搜不到相关的答案,难道是我环境问题吗?(加不加sudo结果都一样)
脚本开头指定执行工具是bash,sh和bash有区别,不是100%兼用。
bash可以模拟sh的行为
Brace expansion introduces a slight incompatibility with historical versions of sh. sh does not treat opening or closing braces specially when they appear as part
of a word, and preserves them in the output. Bash removes braces from words as a consequence of brace expansion. For example, a word entered to sh as file{1,2}
appears identically in the output. The same word is output as file1 file2 after expansion by bash. If strict compatibility with sh is desired, start bash with
the +B option or disable brace expansion with the +B option to the set command (see SHELL BUILTIN COMMANDS below).
bash +B的行为就跟sh差不多了,也就是说,对{1..10}的行为就不是展开为1,2,3,4,5,。。。,10,而是输出{1..10}