I read that the option -a [Archivo]
inside a condition is true if the file exists, but now I run into this and realize the option -a
has nothing to do with the explanation above:
var1=1
var2=2
[ "$var1" -a "$var2" ] && echo true || echo false
The output is true
and I don't have any file named "1" or "2"; Now when one of the two variables or both are empty then:
var1=1
var2=""
[ "$var1" -a "$var2" ] && echo true || echo false
The output is now false
.
I don't understand what the option is doing -a
. I am reading a code and it uses that type of validation, someone explain to me: What is the function of that option?
Since I didn't find anything else on the internet.
The -a option is an alternative logical AND to &&. There is also -o as an alternative to ||.
In your example:
The true/false condition is given by the content of the variable or by some logical expression. In the expression:
var1 and var2 evaluate to true if they have any values, so
transforms to [true and true] returns true. That's why it prints "true".
In the second case, var2 has no value, so in a conditional it is false, so the condition:
is [true and false] returns "false".
Very good question but daring (and wrong) interpretation of what you read.
TL;DR
It is not the same
[ -a parametro1 ]
as[ parametro1 -a parametro2 ]
. Bash interprets it in different ways.The first operates on files, and the second as a logical AND operator.
detailed explanation
The key here is in the operator
[
. This operator is an alias of the built-intest
:So when we look at
man 5 bash
, we find thattest
,[
and[[
, allow the evaluation of conditional expressions.In the section
test
, it is specified how expressions will be evaluated if they have 1, 2, 3, 4, 5 or more arguments. In this case, as we have 2 arguments (the parameter-a
and the file), we find that we are talking about unary operators , that is, they receive a single value as a parameter , and they will be evaluated according to what is established aCONDITIONAL EXPRESSIONS
.In the section
CONDITIONAL EXPRESSIONS
, we find this:That is, it returns 0 ( true , or successful exit in Bash's case) if it found the file, or another value if it didn't.
However, when you write something of the form
[ parametro1 -a parametro2 ]
, we are already talking about three arguments , so the operation is binary , in which the first value (parametro1
) and the third value (parametro2
) will be taken as the values to evaluate according to the second value (the operator; in this case-a
).In the same section of
test
, it is stated that when it has the form[ parámetro1 -a parámetro2 ]
, it has a successful exit ("returns" True) if both parameters are true; that is, it works as a logical AND .conclusion
Bash doesn't guess your mind. You have to act according to the manual of Bash and each program.
In Bash, positions, tokenization (split strings according to the value of the variable
IFS
) and syntax matter a lot.Your best friends are the TAB key and the manual (
man bash
,help <builtin>
,<builtin> --help
).