This:
date +%s
will return the number of seconds since the epoch.
This:
date +%s%N
returns the seconds and current nanoseconds.
So:
date +%s%N | cut -b1-13
will give you the number of milliseconds since the epoch - current seconds plus the left three of the nanoseconds.
and from MikeyB - echo $(($(date +%s%N)/1000000))
(dividing by 1000 only brings to microseconds)
I don't think a command or shell builtin for this exists, as it's a trivial subset of what the Bourne shell for
loop is designed for and implementing a command like this yourself is therefore quite simple.
Per JimB's suggestion, use the Bash builtin for generating sequences:
for i in {1..10}; do command; done
For very old versions of bash, you can use the seq
command:
for i in `seq 10`; do command; done
This iterates ten times executing command
each time - it can be a pipe or a series of commands separated by ;
or &&
. You can use the $i
variable to know which iteration you're in.
If you consider this one-liner a script and so for some unspecified (but perhaps valid) reason undesireable you can implement it as a command, perhaps something like this on your .bashrc (untested):
#function run
run() {
number=$1
shift
for i in `seq $number`; do
$@
done
}
Usage:
run 10 command
Example:
run 5 echo 'Hello World!'
Best Answer
in Bash, execution of code after each command can be achieved using the "trap" builtin and the "DEBUG" level.
in bash do: help trap or refer to bash manual page, and look for the trap section
example:
This will execute the command "date" juste after each command.
Of course you can format the timestamp as you want, refer to the "date" manual page.
If you want to remove the newline, so that timestamp appears in front of next command, you can do:
And to terminate this:
You can combine with "set -x" / "set +x"