Artificial intelligent assistant

Why does parameter expansion with spaces without quotes work inside double brackets "[[" but not inside single brackets "["? I'm confused with using single or double brackets. Look at this code: dir="/home/mazimi/VirtualBox VMs" if [[ -d ${dir} ]]; then echo "yep" fi It works perfectly although the string contains a space. But when I change it to single bracket: dir="/home/mazimi/VirtualBox VMs" if [ -d ${dir} ]; then echo "yep" fi It says: ./script.sh: line 5: [: /home/mazimi/VirtualBox: binary operator expected When I change it to: dir="/home/mazimi/VirtualBox VMs" if [ -d "${dir}" ]; then echo "yep" fi It works fine. Can someone explain what is happening? When should I assign double quotes around variables like `"${var}"` to prevent problems caused by spaces?

The single bracket `[` is actually an alias for the `test` command, it's _not_ syntax.

One of the downsides (of many) of the single bracket is that if one or more of the operands it is trying to evaluate return an empty string, it will complain that it was expecting two operands (binary). This is why you see people do `[ x$foo = x$blah ]`, the `x` guarantees that the operand will never evaluate to an empty string.

The double bracket `[[ ]]`, on the other hand, _is syntax_ and is much more capable than `[ ]`. As you found out, it does not have the "missing operand" issue and it also allows for more C-like syntax with `>, <, >=, <=, !=, ==, &&, ||` operators.

My recommendation is the following: If your interpreter is `#!/bin/bash`, then _always_ use `[[ ]]`

It is important to note that `[[ ]]` is not supported by all POSIX shells, however many shells do support it such as `zsh` and `ksh` in addition to `bash`

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 87008ea27c69b4570531c9decb63669d