bash for 循环在命令行中工作,但在脚本中失败
当在 debian bash 命令行中运行 for 语句时,它工作正常。 但是当我在 sh 脚本中运行它或使用 bash 命令运行它时,它会保留报告“意外标记‘do’附近的错误” 差别在哪里呢?
[leon@www] ~/tmp $ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[leon@www] ~/tmp $ bash for i in {1..10}; do echo $i; done
-bash: syntax error near unexpected token `do'
顺便说一句,在 centos 环境下一切正常。
When a run a for statement in debian bash command line, it works fine.
But when I run it in a sh script or run it with bash command, it's keeping report "error near unexpected token `do'"
Where is the difference?
[leon@www] ~/tmp $ for i in {1..10}; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[leon@www] ~/tmp $ bash for i in {1..10}; do echo $i; done
-bash: syntax error near unexpected token `do'
BTW, all works fine in centos enviorment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
-c
选项,以便 bash 从您传入的字符串中读取命令。此外,请在命令周围使用单引号。Use the
-c
option so that bash reads the commands from the string you pass in. Also, use single quotes around the command.你的 bash 命令行以第一个结尾;
所以它单独执行为:
man bash 说命令参数应该是要加载的文件: bash [选项] [文件]
your bash command line ends with the first ;
so it gets executed separately as:
and man bash says command argument should be a file to load: bash [options] [file]
您可以将所有脚本包含在引号内或文件中。因为在这里,您正在执行
bash for i in {1..10}
然后do echo $i
等等。如果不将其放入文件中,则应该使用 -c 选项。You can wrap all your script inside inverted commas or in a file. Because here, you're doing
bash for i in {1..10}
thendo echo $i
and so on. You should use -c option if you don't put it in a file.