zshで 'time'出力をbashのように見えるようにするには?

zshで 'time'出力をbashのように見えるようにするには?

time <command>Bashフォーマット:

$time ./test.sh

real    0m0.000s
user    0m0.006s
sys     0m0.000s

zshから:

 $time ./test.sh                                                       
 ./test.sh  0.01s user 0.00s system 94% cpu 0.007 total

zshに切り替えた後、これは私を悩ませました。timezshの出力をbashのように見せるには?

答え1

timeinキーワードは、zsh変数で指定された形式で出力を生成しますTIMEFMT

この変数のデフォルト値は次のとおりです。

%J  %U user %S system %P cpu %*E total

次のように変更できます。

TIMEFMT=$'%J\n%U user\n%S system\n%P cpu\n%*E total'

(実際には、基本フォーマット文字列に改行文字を挿入するだけです。)

次の種類の出力を提供します。

$ time sleep 2
sleep 2
0.00s user
0.00s system
0% cpu
2.010 total

または、次の場所に近いですbash

$ TIMEFMT=$'real\t%E\nuser\t%U\nsys\t%S'
$ time sleep 2
real    2.02s
user    0.00s
sys     0.01s

TIMEFMTマニュアルの変数のドキュメントを参照してくださいzshparam

存在する私のものシステム(zsh 5.7.1実行)、内容は次のとおりです。

   TIMEFMT
          The format of process time reports with the time keyword.  The
          default is `%J  %U user %S system %P cpu %*E total'.  Recognizes
          the following escape sequences, although not all may be
          available on all systems, and some that are available may not be
          useful:

          %%     A `%'.
          %U     CPU seconds spent in user mode.
          %S     CPU seconds spent in kernel mode.
          %E     Elapsed time in seconds.
          %P     The CPU percentage, computed as 100*(%U+%S)/%E.
          %W     Number of times the process was swapped.
          %X     The average amount in (shared) text space used in
                 kilobytes.
          %D     The average amount in (unshared) data/stack space used in
                 kilobytes.
          %K     The total space used (%X+%D) in kilobytes.
          %M     The  maximum memory the process had in use at any time in
                 kilobytes.
          %F     The number of major page faults (page needed to be
                 brought from disk).
          %R     The number of minor page faults.
          %I     The number of input operations.
          %O     The number of output operations.
          %r     The number of socket messages received.
          %s     The number of socket messages sent.
          %k     The number of signals received.
          %w     Number of voluntary context switches (waits).
          %c     Number of involuntary context switches.
          %J     The name of this job.

          A star may be inserted between the percent sign and flags
          printing time (e.g., `%*E'); this causes the time to be printed
          in `hh:mm:ss.ttt' format (hours and minutes are only printed if
          they are not zero).  Alternatively, `m' or `u' may be used
          (e.g., `%mE') to produce time output in milliseconds or
          microseconds, respectively.

関連情報