1

I need to measure the running time of an application, so I'm messing with TIMEFORMAT in order to print short format with the maximum precision available.

From the Bash Refence Manual:

TIMEFORMAT

The value of this parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word should be displayed. The ‘%’ character introduces an escape sequence that is expanded to a time value or other information. The escape sequences and their meanings are as follows; the braces denote optional portions.

%% A literal ‘%’.

%[p][l]R The elapsed time in seconds.

%[p][l]U The number of CPU seconds spent in user mode.

%[p][l]S The number of CPU seconds spent in system mode.

%P The CPU percentage, computed as (%U + %S) / %R.

The optional p is a digit specifying the precision, the number of fractional digits after a decimal point. A value of 0 causes no decimal point or fraction to be output. At most three places after the decimal point may be specified; values of p greater than 3 are changed to 3. If p is not specified, the value 3 is used.

The optional l specifies a longer format, including minutes, of the form MMmSS.FFs. The value of p determines whether or not the fraction is included.

If this variable is not set, Bash acts as if it had the value

$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'

If the value is null, no timing information is displayed. A trailing newline is added when the format string is displayed.

But, whenever I set TIMEFORMAT the tabs and new lines are not expanding:

rafael@lip ~/time-tests $ time ls

real    0m0.099s
user    0m0.000s
sys 0m0.002s
rafael@lip ~/time-tests $ TIMEFORMAT='\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
rafael@lip ~/time-tests $ time ls
\nreal\t0m0.002s\nuser\t0m0.000s\nsys\t0m0.002s

Why? and how to solve it?

3

The shell does not interpret any backslashes inside single quotes. If you want backslashes to be interprested use the $'...' construct, as in:

TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'

From man bash:

   Words of the form $'string'  are  treated  specially.   The  word
   expands  to string, with backslash-escaped characters replaced as
   specified by the ANSI C standard.  Backslash escape sequences, if
   present, are decoded as follows:
          \a     alert (bell)
          \b     backspace
          \e
          \E     an escape character
          \f     form feed
          \n     new line
          \r     carriage return
          \t     horizontal tab
          \v     vertical tab
          \\     backslash
          \'     single quote
          \"     double quote
          \nnn   the  eight-bit  character  whose value is the octal
                 value nnn (one to three digits)
          \xHH   the eight-bit character whose value is the hexadec‐
                 imal value HH (one or two hex digits)
          \uHHHH the  Unicode  (ISO/IEC 10646) character whose value
                 is the hexadecimal value HHHH (one to four hex dig‐
                 its)
          \UHHHHHHHH
                 the  Unicode  (ISO/IEC 10646) character whose value
                 is the hexadecimal value HHHHHHHH (one to eight hex
                 digits)
          \cx    a control-x character

   The  expanded  result is single-quoted, as if the dollar sign had
   not been present.

By contrast, with plain single quotes, no characters are given any special treatment, as man bash explains:

   Enclosing characters in single quotes preserves the literal value
   of  each  character  within  the  quotes.  A single quote may not
   occur between single quotes, even when preceded by a backslash.

Thus, inside plain single quotes, a backslash is just a backslash.

1

You must use an dolar sign $ in assignment to TIMEFORMAT:

TIMEFORMAT=$'\n\nreal_test\t%5lR\nuser_test\t%5lU\nsys_test\t%5lS'  
time ls
....

real_test   0m0.006s
user_test   0m0.000s
sys_test    0m0.004s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.