1

When I run the code below:

if [[ -v xxx ]]; then
    echo "OK"
fi

Error happens:

-bash: conditional binary operator expected
-bash: syntax error near `xxx'

It happens in the login shell. When I run

bash

in the same login shell, then run the code above, the error disappears.

The OS is macOs Catalina, shell login command is

login -pf <username> /bin/bash
Dg W
  • 13
  • 3
  • Do you have a question? – Arkadiusz Drabczyk Aug 24 '20 at 10:19
  • 1
    Show your code. No xxx. – Cyrus Aug 24 '20 at 10:29
  • 'xxx' is the variable name I used when testing. It also happens when source bash_completion script. ```-bash: /usr/local/share/bash-completion/bash_completion: line 251: conditional binary operator expected -bash: /usr/local/share/bash-completion/bash_completion: line 251: syntax error near `exclude' -bash: /usr/local/share/bash-completion/bash_completion: line 251: ` if [[ -v exclude ]]; then' ``` – Dg W Aug 24 '20 at 10:53
  • The question is why the error happens and how to fix or avoid it. – Dg W Aug 24 '20 at 10:55
  • Your code works fine in my Bash. – ceving Aug 24 '20 at 11:03
  • Are you sure the error you you're getting pertains exactly to the code snippet you're showing? Perhaps show some context in your script that has the error. – lurker Aug 24 '20 at 11:15
  • More information: It's on macOS, in the login shell opened with command 'login -pf /bin/bash' – Dg W Aug 24 '20 at 11:41
  • `-v` is an operator. The error implies that operator isn't present, and that the code you're running isn't really identical to that given in the question. – Charles Duffy Aug 24 '20 at 11:54
  • That said... where I'd start is putting `set -x` earlier in your script to trace its execution, and editing that trace into the question. – Charles Duffy Aug 24 '20 at 11:55

2 Answers2

2

Running login -pf <username> /bin/bash on MacOS Catalina will put you into bash version 3.2.57(1).

In this version of bash, -v is not a valid operator for [[ ]].

You likely normally use a more updated version of bash. Try echo $BASH_VERSION while running each shell.

See this relevant SO question, How to check if a variable is set in bash, where it is discussed that the -v option was available in version 4.2.

dwmorrin
  • 2,704
  • 8
  • 17
  • Thank you! The login bash version is 3.2.57, and the bash i usually use is 5.0.18, installed by homebrew. – Dg W Aug 25 '20 at 06:32
0

For bash in v3, install bash_completion, for bash in v4.2 and above, install bash_completion@2. If you have upgraded the bash on your mac, please make sure you update your default bash to the newer one, check this link for the steps.