On Mon, 24 Jan 2022 17:26:36 -0800,
hongy...@gmail.com wrote:
> On Tuesday, January 25, 2022 at 12:04:43 AM UTC+8, John-Paul Stewart wrote:
>> On 2022-01-23 20:48, Keith Thompson wrote:
>> > "
hongy...@gmail.com" <
hongy...@gmail.com> writes:
>> >> See the following error:
>> >>
>> >> $ test ! command -v git
>> >> bash: test: -v: binary operator expected
>> >
>> > As expected, since `test` has no `-v` operator.
>> That depends on which 'test' you're using. The bash built-in 'test'
>> (which the OP appears to be using) does have a (unary) -v operator.
>> From 'help test' in bash:
>>
>> -v VAR True if the shell variable VAR is set.
>>
>> For which the OP's syntax is (also) obviously wrong.
>
> I'm not using the -v operator 'test' command, instead, here is the -v parameter of 'command':
No, you are NOT using the -v parameter of 'command'. That may have been
your intention, but that's NOT what you did.
The builtin test command treats it's parameters as an expression.
In your case
test ! command -v git
you gave test four parameters:
a '!', instructing test to /complement/ the results of the
following expression
a string 'command', which would be the left hand side of
a "binary" expression
a string '-v', which would be the "operator" of a "binary"
expression, and
a string 'git', which would be the right hand side of a
"binary" expression
By issuing the error message
bash: test: -v: binary operator expected
bash tells you that the builtin[1] test encountered an unexpected
and invalid '-v' operator (instead of one of the "known" binary
operators) between the 'command' string and the 'git' string.
[1] by issuing the error as "bash: test:", bash has confirmed
that you invoked the bash builtin test. Had you used the test(1)
binary, bash would not have inserted the "bash: " into the error
message, and instead issued
test: '-v': binary operator expected
HTH
--
Lew Pitcher
"In Skills, We Trust"