Skip to content

The array or string type of the PROMPT_COMMAND variable is not respected by mcfly init bash (and in fact erases it if it's an array) #461

@pmarreck

Description

@pmarreck

I have multiple Bash shell extensions that mess with PROMPT_COMMAND:
bash-preexec, mcfly, direnv, and starship.

PROMPT_COMMAND is either a Bash array (ideally), or a string with ";" separators. The former is preferred, but the latter should be respected, if that's how it was initialized.

https://github.com/rcaloras/bash-preexec is a system to manage preexec functions similarly to zsh, by setting up a Bash array of precmd_functions that are run from PROMPT_COMMAND.

Anyway, both Starship (I will be reporting this bug there too) and mcfly do not respect a PROMPT_COMMAND that is an array; the logic checks for its existence only (not whether it's a string or an array) and simply overwrites it if [[ ! "$PROMPT_COMMAND" =~ "mcfly_prompt_command" ]] (which obviously is undefined on an array).

Direnv does this more correctly; here is the output of direnv hook bash on my system (NixOS):

_direnv_hook() {
  local previous_exit_status=$?;
  trap -- '' SIGINT;
  eval "$("/nix/store/l97wg9p8zazih2r45g5fbz9b93rrj7p4-direnv-2.34.0/bin/direnv" export bash)";
  trap - SIGINT;
  return $previous_exit_status;
};
if [[ ";${PROMPT_COMMAND[*]:-};" != *";_direnv_hook;"* ]]; then
  if [[ "$(declare -p PROMPT_COMMAND 2>&1)" == "declare -a"* ]]; then
    PROMPT_COMMAND=(_direnv_hook "${PROMPT_COMMAND[@]}")
  else
    PROMPT_COMMAND="_direnv_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
  fi
fi

As you can see, it respects PROMPT_COMMAND's that are Bash arrays. Bash 5.1 was the first to allow a PROMPT_COMMAND of type "bash array".

Please fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions