Search code examples
bashlogical-or

logical OR in bash with two function calls in sub conditions


I have a bash function returning bool

_on_host() { [[ $(hostname) =~ ^$1.* ]]; }

It works well. How to to write a function which will answer if I'm on host A OR on host B ?

My failed attempts:

$ juju() { [[ _on_host aaa || _on_host bbb ]];
-bash: conditional binary operator expected
-bash: syntax error near `aaa'

or

$ juju() { [ _on_host x100 -o _on_host borealis ]; }
$ juju
-bash: [: too many arguments

or

juju() { [[ _on_host aaa ]] || [[ _on_host bbb ]]; }
-bash: conditional binary operator expected
-bash: syntax error near `aaa'

Solution

  • You can simply use:

    juju() { _on_host aaa || _on_host bbb; }