bash mac os下bash的“if”语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12069792/
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
"if" statements of bash under mac os
提问by Tom Hymany
If there some difference between the bash of Mac OS and other linuxs'? I wrote a simple bash script named "test.sh" like this:
Mac OS 的 bash 和其他 linux 的 bash 之间是否有一些区别?我编写了一个名为“test.sh”的简单 bash 脚本,如下所示:
#!/bin/bash
MYVAR=abc
if [ $MYVAR = abc ]; then
echo "ok"
fi
When I run it in terminal, some error occurs:
当我在终端中运行它时,发生了一些错误:
./test.sh: line 3: syntax error near unexpected token `then'
./test.sh: line 3: `if[ $MYVAR = abc ]; then'
then I delete the character ";" before "then" and run the script again, some infos occurs:
然后我删除字符“;” 在“then”之前并再次运行脚本,会出现一些信息:
./test.sh: line 3: if[ abc = abc ]: command not found
ok
./test.sh: line 5: syntax error near unexpected token `fi'
./test.sh: line 5: `fi'
Could someone tell me what's wrong with my script?
有人能告诉我我的脚本有什么问题吗?
回答by Michael Krelin - hacker
Consider putting spaces into your file the way you put it in your example (if [).
考虑按照您在示例 ( if [) 中的方式将空格放入文件中。
回答by choroba
[is a command (same as test). It must be separated by spaces on both sides.
[是一个命令(与 相同test)。两边必须用空格隔开。

