シェルスクリプトで `set -x` して実行するコマンドを都度表示するとき、実行時刻も表示できる

最終更新:1 か月前

シェルスクリプトを書くとき、 set -x することで実行されるコマンドを都度表示できるのはよく知られている(?)。

set -x

date
sleep 1
date
$ bash test.sh
+ date
Wed May  1 08:32:59 PM JST 2024
+ sleep 1
+ date
Wed May  1 08:33:00 PM JST 2024

実はzshでは、実行時刻も表示させることができる。

PS4='[%*] + '
set -x

date
sleep 1
date
$ zsh test.sh
[20:34:13] + date
Wed May  1 08:34:13 PM JST 2024
[20:34:13] + sleep 1
[20:34:14] + date
Wed May  1 08:34:14 PM JST 2024

要するにプロンプトを自由に設定できるというわけ。

ちなみに、man 1 zshparamPS4 の説明があって、

PS4 <S>

The execution trace prompt. Default is ‘+%N:%i> ‘, which displays the name of the current shell structure and the line number within it. In sh or ksh emulation, the default is ‘+ ‘.

man 1 zshmisc の「Simple Prompt Escapes」の節%* の説明がある。

%*

Current time of day in 24-hour format, with seconds.


……もうそろそろ、他人が使う可能性があるシェルスクリプトをzshで書いてもいいんですかねえ。

Xで コメントする

Mentions