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`
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.
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.
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 .
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 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
To exit the command, press CTRL+C. Here, the 'uptime' command will run and display the updated results every 2 seconds by default.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With