From bc9e5d466daac8dd0b5214ed17f822cde68ebfed Mon Sep 17 00:00:00 2001 From: Eden Tsai Date: Thu, 28 Jan 2021 18:10:58 +0800 Subject: [PATCH] fix: unbound variables in bash completion when `set -o nounset` in Bash, the warnings of unbound variables will break the bash completion. for example: ```sh $ set -o nounset $ my-cli -bash: BASH_COMP_DEBUG_FILE: unbound variable ``` --- bash_completions.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bash_completions.go b/bash_completions.go index 846636d75..ea01a14cf 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -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 } @@ -214,7 +214,7 @@ __%[1]s_handle_reply() completions=("${commands[@]}") if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then completions+=("${must_have_one_noun[@]}") - elif [[ -n "${has_completion_function}" ]]; then + elif [[ -n "${has_completion_function:-}" ]]; then # if a go completion function is provided, defer to that function __%[1]s_handle_go_custom_completion fi @@ -334,7 +334,7 @@ __%[1]s_handle_command() __%[1]s_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}" local next_command - if [[ -n ${last_command} ]]; then + if [[ -n ${last_command:-} ]]; then next_command="_${last_command}_${words[c]//:/__}" else if [[ $c -eq 0 ]]; then