Skip to content

Commit a267adb

Browse files
committed
fix: unbound variables in bash completion
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.
1 parent b55fa79 commit a267adb

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

bash_completions.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func writePreamble(buf io.StringWriter, name string) {
2424
WriteStringAndCheck(buf, fmt.Sprintf(`
2525
__%[1]s_debug()
2626
{
27-
if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then
27+
if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then
2828
echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
2929
fi
3030
}
@@ -187,7 +187,7 @@ __%[1]s_handle_reply()
187187
PREFIX=""
188188
cur="${cur#*=}"
189189
${flags_completion[${index}]}
190-
if [ -n "${ZSH_VERSION}" ]; then
190+
if [ -n "${ZSH_VERSION:-}" ]; then
191191
# zsh completion needs --flag= prefix
192192
eval "COMPREPLY=( \"\${COMPREPLY[@]/#/${flag}=}\" )"
193193
fi
@@ -272,7 +272,7 @@ __%[1]s_handle_flag()
272272
273273
# if a command required a flag, and we found it, unset must_have_one_flag()
274274
local flagname=${words[c]}
275-
local flagvalue
275+
local flagvalue=""
276276
# if the word contained an =
277277
if [[ ${words[c]} == *"="* ]]; then
278278
flagvalue=${flagname#*=} # take in as flagvalue after the =
@@ -291,7 +291,7 @@ __%[1]s_handle_flag()
291291
292292
# keep flag value with flagname as flaghash
293293
# flaghash variable is an associative array which is only supported in bash > 3.
294-
if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
294+
if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
295295
if [ -n "${flagvalue}" ] ; then
296296
flaghash[${flagname}]=${flagvalue}
297297
elif [ -n "${words[ $((c+1)) ]}" ] ; then
@@ -363,7 +363,7 @@ __%[1]s_handle_word()
363363
__%[1]s_handle_command
364364
elif __%[1]s_contains_word "${words[c]}" "${command_aliases[@]}"; then
365365
# aliashash variable is an associative array which is only supported in bash > 3.
366-
if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
366+
if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
367367
words[c]=${aliashash[${words[c]}]}
368368
__%[1]s_handle_command
369369
else
@@ -402,8 +402,8 @@ func writePostscript(buf io.StringWriter, name string) {
402402
local commands=("%[1]s")
403403
local must_have_one_flag=()
404404
local must_have_one_noun=()
405-
local has_completion_function
406-
local last_command
405+
local has_completion_function=""
406+
local last_command=""
407407
local nouns=()
408408
409409
__%[1]s_handle_word
@@ -605,7 +605,7 @@ func writeCmdAliases(buf io.StringWriter, cmd *Command) {
605605

606606
sort.Strings(cmd.Aliases)
607607

608-
WriteStringAndCheck(buf, fmt.Sprint(` if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then`, "\n"))
608+
WriteStringAndCheck(buf, fmt.Sprint(` if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then`, "\n"))
609609
for _, value := range cmd.Aliases {
610610
WriteStringAndCheck(buf, fmt.Sprintf(" command_aliases+=(%q)\n", value))
611611
WriteStringAndCheck(buf, fmt.Sprintf(" aliashash[%q]=%q\n", value, cmd.Name()))

0 commit comments

Comments
 (0)