简体   繁体   中英

How can I make Bash automatically pipe the output of every command to something like tee?

I use some magic in $PROMPT_COMMAND to automatically save every command I run to a database:

PROMPT_COMMAND='save_command "$(history 1)"'

where save_command is a more complicated function. It would be nice to save also the head / tail of the output of each command, but I can't think of a reasonable way to do this, other than manually prepending some sort of shell function to everything I type (and this becomes even more painful with complicated pipelines or boolean expressions). Basically, I just want the first and last 10 lines of whatever went to /dev/tty to get saved to a variable (or even a file) - is there any way to do this?

script(1) will probably get you started. It won't let you just record the first and last 10 lines, but you can do some post-processing on its output.

bash | tee /dev/tty ./bashout

This saves all stdout gets saved to bashout.

bash | tee /dev/tty | tail > ./bashout

The tail of stdout of every command gets written to bashout.

bash | tee /dev/tty | sed -e :a -e '10p;$q;N;11,$D;ba' > ./bashout

The first and last 10 lines of stdout of every command gets written to bashout.

These don't save the command, but if you modify your save_command to print the command to stdout, it will get in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM