You can do this with bash-preexec.
Download the script:
wget 'https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh'
Source the script:
source 'bash-preexec.sh'
Now, according to its usage notes:
Two functions preexec and precmd can now be defined and they'll be automatically invoked by bash-preexec if they exist.
preexec
Executed just after a command has been read and is about to be executed. The string that the user typed is passed as the first argument.
precmd
Executed just before each prompt. Equivalent to PROMPT_COMMAND, but more flexible and resilient.
You can use these like so:
preexec() { echo "just typed $1"; }
precmd() { echo "printing the prompt"; }
It might also be worth mentioning that bash-preexec is just a shell script which is implemented using the DEBUG
trap and the PROMPT_COMMAND
environment variable.
For more information about the DEBUG
trap see the reference documentation for the trap
command:
For more information about the PROMPT_COMMAND
environment variable refer to the Bash manual:
alias
could be a solution:alias echo="echo you typed: "
, soecho here we go
will result inyou typed: here we go
. But I am not sure how it will behave overwriting bash build in commands. And it won't repeat your command this way ... – ChristophS Jun 6 '17 at 15:46