Skip to content

Commit

Permalink
fix: unbound variables in bash completion
Browse files Browse the repository at this point in the history
when `set -o nounset` in Bash,
the warnings of unbound variables will break the bash completion.

use `kubectl` as example:

```sh
$ set -o nounset
$ my-cli <Tab>-bash: BASH_COMP_DEBUG_FILE: unbound variable
$
```

the warning break bash completion without any completion result,
and cause my cursor move to the newline.

Use `${variable:-}` substitution in Bash ,
that assign an empty string as default for unbound variables to fix the warnings.
  • Loading branch information
edentsai committed Jan 29, 2021
1 parent 9df156e commit 034c8aa
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions bash_completions.go
Expand Up @@ -24,7 +24,7 @@ func writePreamble(buf *bytes.Buffer, name string) {
buf.WriteString(fmt.Sprintf(`
__%[1]s_debug()
{
if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then
if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then
echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
fi
}
Expand Down Expand Up @@ -402,8 +402,8 @@ func writePostscript(buf *bytes.Buffer, name string) {
local commands=("%[1]s")
local must_have_one_flag=()
local must_have_one_noun=()
local has_completion_function
local last_command
local has_completion_function=""
local last_command=""
local nouns=()
__%[1]s_handle_word
Expand Down

0 comments on commit 034c8aa

Please sign in to comment.