-1

I can use blank to mean NULL in test and in an arithmetic expansion ((...)), for example

$ test ; echo $?
1
$ (( )); echo $?
1

But with the extended test command, I get an error,

$ [[ ]]; echo $?
bash: syntax error near `;'

OK, I may fix this error by using '' or "", but my question are

  1. Is there anyway I can use blank test [[ ]] without an error?
  2. If the answer from 1. is "no", then I would like to know a reason why [[ ]] behaves differently from [ ] and (( ))?
fronthem
  • 4,011
  • 8
  • 34
  • 55

1 Answers1

1

For your 1st question why only [[ ]] don't work when I read man page for BASH I see following:

[[…]] [[ expression ]] Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below in Bash Conditional Expressions. Word splitting and filename expansion are not performed on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed. Conditional operators such as ‘-f’ must be unquoted to be recognized as primaries.

Which means it shouldn't be empty.

For your second question difference between [ and [[ is [[ is enhancement of [ here is very useful SO link I found you could go through it too.

What's the difference between [ and [[ in Bash?

EDIT: For checking last command's status a simple check I created is which you could take it as a start too:

if [[ $? -eq 0 ]]
then
  echo "Last command ran successfully."
else
  echo "Last command status is NOT 0 so could be it did not run properly."
fi

EDIT2: Adding example for (( too as per OP's question in comments. Example of use of ((.

cat script.ksh
echo "Please enter a value:"
read value
if ! ((value % 4)); then
    echo "$value is fully divided from 4."
fi

(( from man BASH:

((expression)) The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non- zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93