Skip to content

Commit 7093d5c

Browse files
authored
fix(bash): Handle Unbound Variables Errors in Bash (starship#4972)
* fix: unbound bp pipestatus variable * fix: unbound preserved prompt command variable * fix: unbound starship start time variable * fix: unbound preexec_functions, precmd_functions and PROMPT_COMMAND variables.
1 parent da4af64 commit 7093d5c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/init/starship.bash

+7-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ starship_precmd() {
3636
if [[ ${BLE_ATTACHED-} && ${#BLE_PIPESTATUS[@]} -gt 0 ]]; then
3737
STARSHIP_PIPE_STATUS=("${BLE_PIPESTATUS[@]}")
3838
fi
39-
if [[ "${#BP_PIPESTATUS[@]}" -gt "${#STARSHIP_PIPE_STATUS[@]}" ]]; then
39+
if [[ -n "${BP_PIPESTATUS-}" ]] && [[ "${#BP_PIPESTATUS[@]}" -gt "${#STARSHIP_PIPE_STATUS[@]}" ]]; then
4040
STARSHIP_PIPE_STATUS=(${BP_PIPESTATUS[@]})
4141
fi
4242

@@ -65,11 +65,13 @@ starship_precmd() {
6565
# command pipeline, which may rely on it.
6666
_starship_set_return "$STARSHIP_CMD_STATUS"
6767

68-
eval "$_PRESERVED_PROMPT_COMMAND"
68+
if [[ -n "${_PRESERVED_PROMPT_COMMAND-}" ]]; then
69+
eval "$_PRESERVED_PROMPT_COMMAND"
70+
fi
6971

7072
local -a ARGS=(--terminal-width="${COLUMNS}" --status="${STARSHIP_CMD_STATUS}" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --jobs="${NUM_JOBS}")
7173
# Prepare the timer data, if needed.
72-
if [[ $STARSHIP_START_TIME ]]; then
74+
if [[ -n "${STARSHIP_START_TIME-}" ]]; then
7375
STARSHIP_END_TIME=$(::STARSHIP:: time)
7476
STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME))
7577
ARGS+=( --cmd-duration="${STARSHIP_DURATION}")
@@ -90,7 +92,7 @@ if [[ ${BLE_VERSION-} && _ble_version -ge 400 ]]; then
9092
blehook PRECMD!='starship_precmd'
9193
# If the user appears to be using https://github.com/rcaloras/bash-preexec,
9294
# then hook our functions into their framework.
93-
elif [[ "${__bp_imported:-}" == "defined" || $preexec_functions || $precmd_functions ]]; then
95+
elif [[ "${__bp_imported:-}" == "defined" || -n "${preexec_functions-}" || -n "${precmd_functions-}" ]]; then
9496
# bash-preexec needs a single function--wrap the args into a closure and pass
9597
starship_preexec_all(){ starship_preexec "$_"; }
9698
preexec_functions+=(starship_preexec_all)
@@ -111,7 +113,7 @@ else
111113

112114
# Finally, prepare the precmd function and set up the start time. We will avoid to
113115
# add multiple instances of the starship function and keep other user functions if any.
114-
if [[ -z "$PROMPT_COMMAND" ]]; then
116+
if [[ -z "${PROMPT_COMMAND-}" ]]; then
115117
PROMPT_COMMAND="starship_precmd"
116118
elif [[ "$PROMPT_COMMAND" != *"starship_precmd"* ]]; then
117119
# Appending to PROMPT_COMMAND breaks exit status ($?) checking.

0 commit comments

Comments
 (0)