【发布时间】:2023-01-24 15:11:05
【问题描述】:
文件.sh
#!/bin/bash
if { true }
then echo "hi"
fi
我在 shell 脚本中有这个。尝试运行时,我得到:
% ./file.sh
./brackets.sh: line 4: syntax error near unexpected token `then'
./brackets.sh: line 4: `then echo "hi"'
在我的 shell 中运行:
% #!/bin/bash
if { true }
then echo "hi"
fi
zsh: event not found: /bin/bash
在我的 shell 中再次运行:
% if { true }
then echo "hi"
fi
hi
所以问题出在!/bin/bash。有人知道发生了什么事吗?我在网上看了看,但上面的所有帖子都已经过去很久了,而且用户似乎没有在文件/shellscript 中这样做。
我在 MacOS Ventura 上运行 zsh。
【问题讨论】:
-
{ true }并不代表你认为的那样;}是true命令的参数。 -
输入有效的 shebang 并将您的脚本粘贴到 shellcheck.net 以进行验证/推荐。没有人在做您所做的事情,因为它不是有效的 bash 语法...