On Mon, Aug 06, 2018 at 04:14:49PM +0530, anant garg wrote:
> $ mode=!
> $ [ "$mode" != "ro" -a "$mode" != "rw" ] && echo OK
> + '[' '!' '!=' ro -a '!' '!=' rw ']'
> bash: [: too many arguments

The use of -a and -o as conjunctions inside a test or [ command is to
be avoided.  It's not POSIX compatible, and as you have now observed
first-hand, even in bash, it is perilous.

For this particular check, I would use case:

  case $mode in
    ro|rw) .... ;;
    *)     .... ;;
  esac

But if for some reason you've got an if-boner, you could either use
two separate test commands:

  if [ "$mode" != ro ] && [ "$mode" != rw ]

or use bash's [[ command:

  if [[ $mode != ro && $mode != rw ]]

The former is POSIX comptaible.  The latter is, of course, a bash extension.

Reply via email to