Skip to main content
more explanations, responding to comment
Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

You can access the just-executed command line with the history built-in. (I have no idea why history 1 prints the just-executed command line but fc -nl -1 prints the previous commmand, as does fc -nl 0.)

PROMPT_COMMAND='echo -en "\033[38;5;2m"; history 1; echo -en "\033[0m\n"'

This prints a number before the command text. Here's a version that removes the number. (It may be incorrect if you go beyond 99999 history lines, I don't know how bash formats the number then.)

prompt_function () {
  local prompt_history="$(history 1)"
  prompt_history=${prompt_history:7}
  echo -En $'\033[38;5;2m'"$prompt_history"$'\033[0m\n'
}
PROMPT_COMMAND=prompt_function

(Note that echo -en ..."$prompt_history"... would expand backslashes in the command line, hence my avoidance ofso I use echo -eE and let the shell expansion generate the control characters with $''.).

You can access the just-executed command line with the history built-in. (I have no idea why history 1 prints the just-executed command line but fc -nl -1 prints the previous commmand, as does fc -nl 0.)

prompt_function () {
  local prompt_history="$(history 1)"
  prompt_history=${prompt_history:7}
  echo -En $'\033[38;5;2m'"$prompt_history"$'\033[0m\n'
}
PROMPT_COMMAND=prompt_function

(Note that echo -en ..."$prompt_history"... would expand backslashes in the command line, hence my avoidance of echo -e.).

You can access the just-executed command line with the history built-in. (I have no idea why history 1 prints the just-executed command line but fc -nl -1 prints the previous commmand, as does fc -nl 0.)

PROMPT_COMMAND='echo -en "\033[38;5;2m"; history 1; echo -en "\033[0m\n"'

This prints a number before the command text. Here's a version that removes the number. (It may be incorrect if you go beyond 99999 history lines, I don't know how bash formats the number then.)

prompt_function () {
  local prompt_history="$(history 1)"
  prompt_history=${prompt_history:7}
  echo -En $'\033[38;5;2m'"$prompt_history"$'\033[0m\n'
}
PROMPT_COMMAND=prompt_function

(Note that echo -en ..."$prompt_history"... would expand backslashes in the command line, so I use echo -E and let the shell expansion generate the control characters with $''.).

Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

You can access the just-executed command line with the history built-in. (I have no idea why history 1 prints the just-executed command line but fc -nl -1 prints the previous commmand, as does fc -nl 0.)

prompt_function () {
  local prompt_history="$(history 1)"
  prompt_history=${prompt_history:7}
  echo -En $'\033[38;5;2m'"$prompt_history"$'\033[0m\n'
}
PROMPT_COMMAND=prompt_function

(Note that echo -en ..."$prompt_history"... would expand backslashes in the command line, hence my avoidance of echo -e.).