brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: TIMEFORMAT makes the bash time keyword print one tidy line

Bash
~/tips/tip-bash-timeformat-time

time rsync -a /srv/data/ /mnt/backup/data/

Get a short, readable timing for any command or block in a script.

time in bash is a keyword, not the /usr/bin/time program, and its three-line real, user and sys report is a lot for a log file. You can shape it with one variable.

Set TIMEFORMAT and time prints your format instead:

TIMEFORMAT='backup took %1R seconds'
time rsync -a /srv/data/ /mnt/backup/data/
# backup took 42.3 seconds

%R is the wall-clock time, %U user CPU time and %S system CPU time. The digit sets the number of decimals, and l gives the long form with minutes, like %3lR.

Because it is a keyword, it can time a whole block or a pipeline at once:

time {
  dump_database
  compress_dump
}

I find that nicer than storing two timestamps and doing the sum yourself.

The report goes to stderr of the shell, not of the command. A redirect after the command, like time make 2> err.log, does not catch it; wrap it as { time make; } 2> timing.log instead. And TIMEFORMAT only affects the bash keyword; /usr/bin/time or command time has its own -f format.

Further reading: Bash manual: Pipelines (time), Bash manual: Bash Variables (TIMEFORMAT)

More Bash tips echo {01..10} cmd > file 2>&1 export HISTIGNORE='ls*:pwd:cd*' #rsync -av --delete /srv/media/ /mnt/backup/media/ APP_HOST=example.com All Bash tips (52) All topics

Link