Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute command after every command in bash

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

This could help me understand how much a command took to execute when I am away from keyboard.

I know 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 could be possible to run this command after every command I execute on bash.

I would also be interested in running the same command before every command so I could know how long a command took.

Is it possible?

What file should I edit?

For example:

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

I would be happy, if I could also introduce this following feauture:

$ 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 ran the program, second is when program terminated the third is self-explanatonary.

As you understood, I don't want to type every time

$ wget google.com && `DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`
like image 533
Jacquelyn.Marquardt Avatar asked Jul 15 '17 21:07

Jacquelyn.Marquardt


People also ask

Does bash execute sequentially?

Yes, they are executed sequentially. However, if you run a program in the background, the next command in your script is executed immediately after the backgrounded command is started.

How do I run one command after another in Linux?

Using the && Operator. We should note that the && operator executes the second command only if the first one returns an exit code of 0. If we want to run the next script, even if the first one fails, we can replace the && operator with the; operator, which runs the next command regardless of the exit code.

How do you execute a command in bash script?

Normally, we do not need to do anything special to execute a command inside of a Bash script. You just write the command the same way you would in your own terminal. Look at the following example where we execute three commands inside of our Bash script – echo, uptime, and who .

How do I run multiple commands at once in Linux?

Run command all at once. To run several commands all at once by putting an ampersand & at the end of the command line. For example start backup script: # /root/ftpbackup.sh &. Now you don’t have to wait finishing /root/ftpbackup.sh script.

How to run a Linux command every x seconds forever?

How to Run or Repeat a Linux Command Every X Seconds Forever 1. Use watch Command Watch is a Linux command that allows you to execute a command or program periodically and also... 2. Use sleep Command

How do I exit the command'uptime'command?

To exit the command, press CTRL+C. Here, the 'uptime' command will run and display the updated results every 2 seconds by default.


1 Answers

To execute a cmd before every command entered, set a trap on DEBUG. Eg.

trap date DEBUG

To execute that command before emitting a prompt, set PROMPT_COMMAND:

PROMPT_COMMAND=date
like image 129
William Pursell Avatar answered Sep 29 '22 11:09

William Pursell