Skip to content

Commit 09ae38e

Browse files
edentsaiumarcor
authored andcommitted
fix: unbound variables in bash completion (spf13#1321)
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 19938aa commit 09ae38e

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
@@ -278,7 +278,7 @@ __%[1]s_handle_flag()
278278
279279
# if a command required a flag, and we found it, unset must_have_one_flag()
280280
local flagname=${words[c]}
281-
local flagvalue
281+
local flagvalue=""
282282
# if the word contained an =
283283
if [[ ${words[c]} == *"="* ]]; then
284284
flagvalue=${flagname#*=} # take in as flagvalue after the =
@@ -297,7 +297,7 @@ __%[1]s_handle_flag()
297297
298298
# keep flag value with flagname as flaghash
299299
# flaghash variable is an associative array which is only supported in bash > 3.
300-
if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
300+
if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then
301301
if [ -n "${flagvalue}" ] ; then
302302
flaghash[${flagname}]=${flagvalue}
303303
elif [ -n "${words[ $((c+1)) ]}" ] ; then
@@ -369,7 +369,7 @@ __%[1]s_handle_word()
369369
__%[1]s_handle_command
370370
elif __%[1]s_contains_word "${words[c]}" "${command_aliases[@]}"; then
371371
# aliashash variable is an associative array which is only supported in bash > 3.
372-
if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
372+
if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then
373373
words[c]=${aliashash[${words[c]}]}
374374
__%[1]s_handle_command
375375
else
@@ -410,8 +410,8 @@ func writePostscript(buf io.StringWriter, name string) {
410410
local command_aliases=()
411411
local must_have_one_flag=()
412412
local must_have_one_noun=()
413-
local has_completion_function
414-
local last_command
413+
local has_completion_function=""
414+
local last_command=""
415415
local nouns=()
416416
local noun_aliases=()
417417
@@ -621,7 +621,7 @@ func writeCmdAliases(buf io.StringWriter, cmd *Command) {
621621

622622
sort.Strings(cmd.Aliases)
623623

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

0 commit comments

Comments
 (0)