Sometime back, we had a requirement to log commands run on the terminal in the Bash shell. This was solved by means of a startup script with essentially the following contents:
#!/bin/bash
SESSION_LAST_COMMAND=
log_last_command() {
local command
command=$(history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//")
if [[ -n "$SESSION_LAST_COMMAND" ]]; then
logger -p local3.notice "shell user [$$]: Executed command: $command"
fi
SESSION_LAST_COMMAND="$command"
}
export PROMPT_COMMAND=log_last_command
It works fine except that pressing ENTER logs the last command again. This happens as many times as the enter key is pressed. And some users do have this habit! Is there a way to avoid these extra logs? Also, an obvious expectation from the solution will that a distinction between just pressing enter a few times and actually re-running the command be possible.