1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

意図しないディレクトリでの Claude Code 起動を防ぐラッパー関数 (Bash / zsh)

1
Posted at

想定環境: Bash (Git Bash 含む)、zsh

Claude Code (claude コマンド) を意図しないディレクトリで起動すると、起動するだけでディレクトリ情報が ~/.claude.json や ~/.claude/projects に記録されてしまうし、普段のディレクトリと誤解して作業依頼までしてしまうとトークン量を無駄遣いしてしまいます。

そこで、以下のラッパー関数を ~/.bashrc や ~/.zshrc に書いておけば、claude と実行したときに許可ディレクトリならそのまま起動し、許可ディレクトリでないなら「本当に続行しますか?」と確認を挟むことができます。

~/.bashrc や ~/.zshrc に以下を追記
claude_allowed_dirs=(
  # Claude Code 許可ディレクトリをここに書いておく
  "${HOME}/aaa"
  "${HOME}/bbb/ccc"
)
claude() {
  local current_dir
  current_dir=$(pwd)
  for dir in "${claude_allowed_dirs[@]}"; do
    [ "$current_dir" = "$dir" ] && { command claude "$@"; return; }
  done
  echo 'このディレクトリでは Claude Code の起動が想定されていません'
  read -p '本当に続行しますか? (y/n): ' ans
  [ "$ans" = 'y' ] || { echo '中止します'; return; }
  command claude "$@"
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?