hi all,
do yo know what is double "[[" and "]]" means?
Here's an example:
[[ -z $ERROROUTPUT ]] || echo "$TITLE"
thanks,
itik
hi all,
do yo know what is double "[[" and "]]" means?
Here's an example:
[[ -z $ERROROUTPUT ]] || echo "$TITLE"
thanks,
itik
IT's a synonym for test, it returns true or false.
That code fragment means: test if $ERROROUTPUT is zero length || echo "$TITLE"
It doesn't make much sense in a programming context because the || allows the echo part to run regardless - it is a boolean or
i.e., the echo happens whether the first test evaluates true or false...
I beg to differ, "||" is and works as an else...
...testing if .bash_history is a file:
but it is a "test" and I do not really know the difference from a single "[", it may depend on Your shell. I think that in Bash it doesn�t matter.
/L
The form "[ ]" is essentially calling the external "test" command, and thus everything is evaluated by the shell first. That's why you get "test: argument expected" when an unquoted variable is NULL, since the expression evaluates to "[ -z ]".
When using "[ ]", you really need to quote all values/variables. But, with the double-brackets, the test is a shell builtin. So nothing needs to be quoted, since it is not evaluated prior to being tested. Plus, you will save dozens of milliseconds by avoiding an external program. 
Using the double-bracket with && is my favorite way of doing a simple if/then:
[[ 1 == 1 ]] && echo "equal" || echo "not equal"
which is *essentially* the same as
if [[ 1 == 1 ]]; then
echo "equal"
else
echo "not equal"
fi
I say "essentially" because the if/then executes the TRUE or the FALSE clause, never both. The other form can run both if the command after the && fails:
[[ 1 == 1 ]] && ehco "equal" || echo "not equal"
This will always print "not equal" since "ehco" is not a command and fails.
You can also do compounding:
[[ 1 == 1 ]] && { echo "equal"; date; } || echo "not equal"
but don't try to get too clever. A 50-line conditional statement using amperstands and braces is harder to read then plain old if/then/else.
Note that the amperstand can be used after any ordinary command:
grep -q localhost /etc/hosts && echo "found" || echo "not found"
Thanks a bunch for the explanation!
Now I can save hundreds of milliseconds in my scripts 
I had some idea that the quoting rules were different, but wasn't clear about exactly what it meant in practical use. And man bash isn't very helpful, or just too much text for me...
/Lakris
That can amount to an awful lot of time if you do it inside a deeply-nested loop. Suppose you execute the loop surrounding it 100.000 times: 100ms times 100.000 are 10.000 seconds, which are ~3 hours - not bad, yes?
bakunin