I'm trying to store both STDOUT and STDERR from the terminal (and if possible STDIN given by user) in a file for every command.
So i started creating a trap function to execute every command in a edited manner like:
shopt -s extdebug
preexec_invoke_exec () {
[ -n "$COMP_LINE" ] && return # do nothing if completing
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
eval `history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//"` |& tee ~/recent_output.txt
return 1 # This prevent executing of original command
}
trap 'preexec_invoke_exec' DEBUG
and saving the above file and executing
source file.sh
This did the work what i wanted but stopped some commands from executing like
cd ..
The reason for this was piping creates a sub-shell and then executes every command in it. So the main shell remains unaffected.
Even the script functionality of bash i.e
script ~/recent_output.txt
worked but only gives output after you do exit
in in terminal
So, basically i want to store/get the output of previous command executed in the bash terminal. You can help me with any language (golang,python...).