無効な $PATH
ディレクトリを確認するコマンド
Stack Exchangeサイト(How to check for valid $PATH directories, and output them telling whether each one is valid or not?)で紹介されていたコマンド;
while read -d: dir
do
[ -d "$dir" ] || echo "Missing: $dir"
done <<<"${PATH%:}:"
-
read -d: dir
は入力を 変数dir
に読み込み、:
で区切る -
[ -d "$dir" ]
はディレクトリの存在をテストする -
||
は、前の文がfalse
を返した場合にのみ、後続の文を実行する -
<<<"${PATH%:}:"
は、ヒア文字列を使用してループに入力を提供する。"${PATH%:}:"
という形式は、PATH
文字列の後に:
が 1つ続くことを保証する
使ってみる
- 自分の環境の $PATH
943文字もある!!
$ echo $PATH
/Users/nak435/Developer/ESP32/ESP-IDF/esp-idf/components/espcoredump:/Users/nak435/Developer/ESP32/ESP-IDF/esp-idf/components/partition_table:/Users/nak435/Developer/ESP32/ESP-IDF/esp-idf/components/app_update:/Users/nak435/.espressif/tools/xtensa-esp-elf-gdb/14.2_20240403/xtensa-esp-elf-gdb/bin:/Users/nak435/.espressif/tools/riscv32-esp-elf-gdb/14.2_20240403/riscv32-esp-elf-gdb/bin:/Users/nak435/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin:/Users/nak435/.espressif/tools/riscv32-esp-elf/esp-14.2.0_20241119/riscv32-esp-elf/bin:/Users/nak435/.espressif/tools/esp32ulp-elf/2.38_20240113/esp32ulp-elf/bin:/Users/nak435/.espressif/tools/openocd-esp32/v0.12.0-esp32-20241016/openocd-esp32/bin:/Users/nak435/.espressif/python_env/idf5.4_py3.13_env/bin:/Users/nak435/Developer/ESP32/ESP-IDF/esp-idf/tools:/opt/homebrew/opt/mysql@8.4/bin:/opt/homebrew/opt/binutils/bin:/opt/homebrew/opt/llvm@14/bin:/Users/nak435/.local/bin:/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin:/Users/nak435/Original Tools:/Users/nak435/Developer/flutter/bin:/opt/homebrew/bin:/usr/local/opt/tcl-tk/bin:/usr/local/sbin:/opt/homebrew/opt/python@3.11/libexec/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/nak435/.nodebrew/current/bin:/Library/Java/JavaVirtualMachines/liberica-jdk-17-full.jdk/Contents/Home/bin:/Users/nak435/.local/bin
-
:
で改行, ソートして見やすくする
(コピペして使えるように、パス内のスペースをエスケープしておく)
$ echo $PATH | tr ':' '\n' | sort | sed 's/ /\\ /g'
/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin
/bin
/Library/Apple/usr/bin
/Library/Java/JavaVirtualMachines/liberica-jdk-17-full.jdk/Contents/Home/bin
/opt/homebrew/bin
/opt/homebrew/opt/binutils/bin
/opt/homebrew/opt/llvm@14/bin
/opt/homebrew/opt/mysql@8.4/bin
/opt/homebrew/opt/python@3.11/libexec/bin
/opt/homebrew/sbin
/opt/X11/bin
/sbin
/System/Cryptexes/App/usr/bin
/Users/nak435/.local/bin
/Users/nak435/.local/bin
/Users/nak435/.nodebrew/current/bin
/Users/nak435/Developer/flutter/bin
/Users/nak435/Original\ Tools
/usr/bin
/usr/local/bin
/usr/local/opt/tcl-tk/bin
/usr/local/sbin
/usr/sbin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin
重複が見つかる。
- 冒頭のコマンドでチェック
$ while read -d: dir ; do ; [ -d "$dir" ] || echo "Missing: $dir" ; done <<<"${PATH%:}:"
Missing: /opt/homebrew/opt/mysql@8.4/bin
Missing: /usr/local/opt/tcl-tk/bin
Missing: /usr/local/sbin
Missing: /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin
Missing: /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin
Missing: /var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
Missing: /Users/nak435/.nodebrew/current/bin
- alias か function にすると便利かも
alias check_path='while read -d: dir ; do ; [ -d "$dir" ] || echo "Missing: ${dir// /\\\ }" ; done <<<"${PATH%:}:"'
alias show_path="echo $PATH | tr ':' '\n' | sort | sed 's/ /\\\ /g'"
$ check_path
$ show_path
以上