multipass icon indicating copy to clipboard operation
multipass copied to clipboard

BASH_VERSION=5.1.16(1)-release causes syntax error: invalid arithmetic operator (bash_complete / homebrew / macOS)

Open nrser opened this issue 1 year ago • 4 comments

Homebrew on my macOS (12.7 Monterey) has upgraded bash to 5.1.16(1)-release, which sets BASH_VERSION=5.1.16(1)-release in the environment.

$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-apple-darwin20.6.0)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ echo "$BASH_VERSION"
5.1.16(1)-release

Under said bash version the bash_complete script included in the Multipass Homebrew installation emits an error when sourced that looks like

-bash: [[: 5.1.16(1)-release: syntax error: invalid arithmetic operator (error token is ".1.16(1)-release")

I traced it down to the following line:

https://github.com/canonical/multipass/blob/ef40bb593eb7db4e76969be303f54733598d2aa5/completions/bash/multipass#L370

Quoting the use of $BASH_VERSION resolves the error on my system, like

if [[ $(echo "${BASH_VERSION}" | sed s/\\..*//g) -gt 3 ]]; then
    OLD_BASH=false
else
    OLD_BASH=true
fi

I see that Homebrew has bash 5.2.15 available, so upgrading bash may resolve the issue, but I wanted to pass on the information in case this is a general case of "should be quoted" (I'm no bash expert), and for anyone else fruitlessly trying to search for said error.

nrser avatar Oct 21 '23 16:10 nrser

My mistake, the quote-fix does NOT appear to work... I must have gotten confused somehow with various bash sessions. The error is still present, and the issue may not even be at that line. Though omitting the Multipass bash_complete script entirely does remove the error, so I'm relatively sure it is in that file somewhere.

nrser avatar Oct 21 '23 16:10 nrser

It appears that the error is in that block of code, but looks like the sed command is not stripping $BASH_VERSION as it's meant to in this case?

nrser avatar Oct 21 '23 17:10 nrser

Changing to this actually does seem to work:

if [[ $(echo ${BASH_VERSION} | sed 's/\..*//g') -gt 3 ]]; then
    OLD_BASH=false
else
    OLD_BASH=true
fi

nrser avatar Oct 21 '23 17:10 nrser

Hi @nrser

Thanks for reporting and looking into this. Basically, that line is to distill the major version of BASH_VERSION by replacing anything behind the dot with nothing. I think it should be a bug on zsh terminal specifically. Is that the terminal you are on? What you proposed sed 's/\..*//g' (single quote plus remove the \) should work. There are other two as well:

  1. sed "s/\\..*//g", with the just double quotation.
    bash_major_verion=${BASH_VERSION%%.*}
    if [[ $bash_major_verion -gt 3 ]]; then
    ...

Can you try the other two out and let me know? The second option looks better and does not depend on sed.

georgeliao avatar Oct 23 '23 10:10 georgeliao