The following works in Ubuntu but not Yocto (Poky).
root@system:~/# x='abc'
root@system:~/# y=''
root@system:~/# [[ $(echo $x) != '' ]] && echo true
true
root@system:~/# [[ $(echo $y) != '' ]] && echo true
sh: : unknown operand
In Ubuntu the last line returns nothing (as expected). Any ideas why it's throwing an error in Yocto?
The problem seems to be that $(echo $y)
is expanding to an empty string, and then [[
isn't handling it correctly. The solution to that would be to quote the command substitution like
[[ "$(echo "$y")" != '' ]] && echo true
though it's probably better still to use printf than echo so you might do it as
[[ "$(printf '%s' "$y")" != '' ]] && echo true
just in case $y
might end up with special characters that can trip up echo
or similar
Apparently, busybox ash has a rather simplistic implementation of [[
. It is the same as [
except that it expects a ]]
instead of ]
final argument. This misses the point of why [[
can be useful at all: [[
is supposed to be a keyword with special parsing and using it looks more beautiful and avoids various pitfalls (while adding some of its own). I guess they added it so a few more bash scripts run unmodified on busybox ash.
To avoid confusion, I recommend not using [[
in busybox at all. Use [
and quote all command substitutions and parameter expansions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With