I would like to capture the stdout (as sent to a terminal), and perform some processing after it is output on the terminal (for the user).

I want to know how to grab the output, process and make changes accordingly.

(If possible) I want this to happen for every command I type on the terminal.

Edit: After a dialogue in a temporary chat forum it was clear that

  • we should

    • both look for which commands are issued
    • and what output there is from the commands and
  • do something, if a particular command or output is detected.

  • does the other script have output, and if so, where should it go? – Jeff Schaller Jan 12 at 15:12
  • No, it does not have any output. For now it just checks the size of stdout. – 100rabh Jan 12 at 15:13
  • It is not clear, what you are asking to do. I get that you want to process stdout, of a process. Do you want the output of the process to go to the screen, do you want the unprocessed data to go to the screen, … (Please amend question to make clear -1 for now) – ctrl-alt-delor Jan 12 at 17:41
  • I think we made it clear in the chat forum (linked to after my answer) :-) – sudodus Jan 12 at 21:25
  • I realized that it can be done even after the ouput is visible to the user. @sudodus, helped me out in the chat. – 100rabh Jan 13 at 6:23

Well, maybe it is not a good idea to grab the output, process and display the output of every command you type on the terminal.

But you can do it for some particular command via tee

your-command | tee saved-output | processing-tool

When you have seen the result from the processing-tool, you can look at the saved-output for example with cat (if short output) or less (if long output).


$ LANG=C sudo lshw | tee saved-output | grep -i -A2 disk
             description: ATA Disk
             product: KINGSTON SKC300S
             physical id: 0.0.0
             description: ATA Disk
             product: WDC WD4002FYYZ-0
             vendor: Western Digital
$ less saved-output

After a dialogue in a temporary chat forum , I suggest to

It works directly to use vialog like the following example.

vialog 2>&1 | tee saved-output | grep 'docker container ls' && echo 'it was mentioned'

In the real case, I think you would like to start something more advanced than echo 'it was mentioned', but this is only a demo example. And things can be more efficient, if you build the specific checks into vialog and disable the alerts, that are probably not necessary in your case.

Screenshot during the dialogue:

enter image description here

Screenshot after the dialogue:

enter image description here

This way you can easily check if a command that is called is not found:

$ LANGUAGE=C vialog 2>&1 | tee saved-output | grep -i 'Command.*not found' && echo 'it was mentioned'
Command 'docker' not found, but can be installed with:
it was mentioned
$ cat saved-output 
----- start vialog at 2019-01-12 18:57:33 ----------------------------
$ docker container ls

Command 'docker' not found, but can be installed with:

sudo apt install docker.io

$ exit
------- end vialog at 2019-01-12 18:58:01 --- used 28 seconds
  • Or to put it very simply, is it possible to run a script after every command, even after outputting it to the terminal.Does something like a post-command hook exist ? – 100rabh Jan 12 at 15:21
  • 1
    @100rabh, Why do you want this? But yes, almost everything is possible with linux. For example, you can call a subshell, that runs your command and does the post-processing before it returns. – sudodus Jan 12 at 15:26
  • To further simplify, I want to check if a specific command is being executed and based on that run a shell script or a python file. – 100rabh Jan 12 at 15:27
  • @100rabh, Do you want to check for a particular text string in the output from the specific command, or do you want to check if there is a particular process during the execution of the specific command, or something else? – sudodus Jan 12 at 15:30
  • 3
    @100rabh, This may be an x/y problem. Please tell us in very general terms what you want, and I or more likely someone else can suggest a method to get what you want. – sudodus Jan 12 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.