[英]bash shell [[ operator failed with error: "conditional binary operator expected"
我在解析的時候遇到了如下bash shell腳本第一行的錯誤。
if [[ -v VAR_1 ]]; then
VAR_2="$VAR_1/sub_folder"
fi
這個腳本在我同事的 Linux 機器 Bash shell 上運行良好,但在我的 Macbook MacOS 上運行失敗。
錯誤信息是
conditional binary operator expected
事實證明,我的 MacOS 正在運行 zsh 而不是 bash。
為了使此腳本與 bash 和 zsh shell 兼容,請改用以下內容,它符合 POSIX 標准並適用於 zsh 和 bash。
if [ -v VAR_1 ]; then
VAR_2="$VAR_1/sub_folder"
fi
或者
if test -v VAR_1 ; then
原因:
[[ 命令是 Bourne Again Shell (bash) 的一個特性。 它是 [ 命令的擴展版本,也用於 bash 和其他 shell 中的條件測試。
在 Zsh 中,[[ 是一個 shell 關鍵字,但默認情況下它的工作方式與 bash 不同。 默認情況下,它在 Zsh 中用於不同的目的,這就是為什么我收到錯誤“預期的條件二元運算符”。
或者,
我還可以使用 emulate emulate sh
命令告訴 Zsh 模擬 Bourne shell (sh) 的行為,包括 bash。這允許在 Zsh 中使用 [[ 和其他特定於 bash 的命令和語法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.