相关文章推荐
愉快的电池  ·  Studio - 使用 GIT 管理项目·  1 月前    · 
犯傻的蟠桃  ·  Central Repository: ...·  6 月前    · 
捣蛋的金针菇  ·  CSS flex ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
root@system:~/# x='abc'
root@system:~/# y=''
root@system:~/# [[ $(echo $x) != '' ]] && echo 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?

I'm betting that sh is not the same on those two platforms and that on Yocto it's not as fully implemented, I was actually surprised that sh implemented [[ at all, Yocto or otherwise – Eric Renouf Dec 13, 2015 at 1:07 @eric-renouf Any idea exactly what the problem can be though, considering it works in Yocto for $x? – geotheory Dec 13, 2015 at 12:02 I'm guessing the problem is how it handles "empty" args, so as long as you have a value on each side of != it will work. You might just try quoting the command substitutions like [[ "$(echo $y)" != '' ]]... and see if that helps – Eric Renouf Dec 13, 2015 at 12:25

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.