Run command after every command in bash

I want to print the date after every bash command I run.

This might help me figure out how much command it took when I left the keyboard.

I know what I could do

`DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`

      

to get the date, but I don't know how or even if it would be possible to run this command after every command I have executed on bash.

I would also be interested to run the same command before each command so that I can know how long it took.

Is it possible?

Which file should I change?

For example:

$ wget google.com
15/07/2017 23:40:05

      

I would be glad if I could also provide the following:

$ wget google.com
15/07/2017 23:40:05 15/07/2017 23:40:11 
Program run for 00:00:06

      

where the first date is when I started the program, secondly when the program is finished, the third is self-evident.

As you understood, I do not want to enter every time

$ wget google.com && `DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`

      

+3


source to share


4 answers


To run a cmd before each command entered, set the hook to DEBUG. For example.

trap date DEBUG

      



To execute this command before issuing a request, set PROMPT_COMMAND:

PROMPT_COMMAND=date

      

+4


source


It does exactly that:

PROMPT_COMMAND+=$'\n'"date +%d/%m/%Y\ %H:%M:%S"

      



The line in PROMPT_COMMAND is evaluated after each command. You just need to add the date command to what you already have. ( $'\n'

(newline) is a slightly more reliable joiner than ;

that since two consecutive ones ;

might give you a syntax error)

+3


source


You can add date and time to your tooltip using a variable PS1

. You can use the command date

, but it is more efficient to use supported special characters such \d

as date or \D{strftime-fmt}

.

For example:

PS1='\u@\h[\D{%F} \D{%T}]\w\$ '

      

or, with color:

PS1='\[\033[01;32m\]\u@\h\[\033[00m\][\[\033[02;33m\]\D{%F}\[\033[08m\]T\[\033[00m\]\[\033[02;33m\]\D{%T}\[\033[00m\]]\[\033[01;34m\]\w\[\033[00m\]\$ '

      

will show:

user@host[2017-07-16 00:01:17]~/somedir$

      

Note that in the second case (with color) we have a valid ISO8601 timestamp, with a "hidden" date / time separator T

in the middle. If you select it with the mouse, it T

will be visible and can be copied. (Also double-clicking will select the full timestamp, not just the date or time.)

+2


source


To print a timestamp after each command, simply change the PS1 prompt and add the date to it. The only catch is that it will tell you the time when the team ended and a new prompt appeared. So if you have a prompt for a long time, just press Enter to capture the startup time before running your command.

PS1="\D{%F %T} \$ "

      

See this wiki arch page or just google bash hint customization.

To add the time it took to execute the program, just add the time before the command

$ time wget google.com

      

This will give you a result like this

real 0m0.177s
user 0m0.156s
sys 0m0.020s

      

And you can get even more lazy and for commands you don't want to type every time you run it, just create an alias.

alias wget="time wget"

      

Since aliases are run before other commands in bash, you can do it this way, even if it looks like recursion. Then you will call it what you are used to.
And of course, aliases and hints can be put into your .bashrc file, so you don't have to type them every time you open a terminal.

0


source







All Articles