mac historyから番号を指定してコマンドを実行する

  • 作成日 2024.12.03
  • mac
mac historyから番号を指定してコマンドを実行する

macのターミナル上でhistoryから番号を指定してコマンドを実行する手順を記述してます。「!」に履歴番号を指定することで可能です。

環境

  • OS macOS Monterey
  • SHELL /bin/zsh

historyから番号を指定してコマンドを実行

historyから番号を指定してコマンドを実行するには、「!履歴番号」を使用します。

まずは履歴を確認します。

history

<出力結果>
   39  ls -la
   40  cd ~
   41  touch test.html
   42  top
   43  du -h
   44  cd -
   45  pwd
   46  history
   47  ls -lh
   48  vi test.html
   49  vi test.html
   50  ls -l
   51  cd hoge
   52  ls
   53  cd ..
   54  seq 10

例えば、39番の「ls -la」を実行する場合は、以下のようになります。

!39

<出力結果>
ls -la