Question Details

No question body available.

Tags

bash shell

Answers (1)

Accepted Answer Available
Accepted Answer
March 20, 2026 Score: 2 Rep: 14,618 Quality: High Completeness: 80%

TL;DR Add a semicolon between the variable and the command to change the scope of the variable. Without it the variable is a temporary one

TIMEFORMAT='%R'; time sleep 1.23; echo "$TIMEFORMAT" 1.231 %R

Also, Bash is executing /usr/bin/time instead of the time keyword when the variable is prepended to the command without a semicolon

If the reserved word time precedes the pipeline, Bash prints timing statistics for the pipeline once it finishes. ... Providing time as a reserved word permits the timing of shell builtins, shell functions, and pipelines. An external time command cannot time these easily.

TIMEFORMAT='%R' time sleep 1.23 0.00user 0.00system 0:01.23elapsed 0%CPU (0avgtext+0avgdata 3952maxresident)k 0inputs+0outputs (0major+104minor)pagefaults 0swaps

same as

/usr/bin/time sleep 1.23 0.00user 0.00system 0:01.23elapsed 0%CPU (0avgtext+0avgdata 4088maxresident)k 0inputs+0outputs (0major+106minor)pagefaults 0swaps

Passing the command to bash -c runs the command that picks the variable up

TIMEFORMAT='%R' bash -c 'time sleep 1.23' 1.231