1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Macでlsやviコマンドが使えなくなってしまったとき

Last updated at Posted at 2024-09-10

問題

.zshrcファイルを修正していたら、ls、viなどのシステムの標準コマンドが一部使えなくなってしまった

> zsh: command not found: ls

環境

Mac book Air M2チップ
シェルはzsh

原因

export PATH=...の最後に/:$PATHを書き忘れるとlsなどのコマンドが使えなくなるらしい

NGexport PATH="/opt/homebrew/opt/openssl/bin:$PATH"
OKexport PATH="/opt/homebrew/opt/openssl/bin"

PATH 環境変数を「追記」ではなく「上書き」してしまい、標準コマンドへのパス設定を消されてしまっていた

結果

  1. /usr/bin/vi ~/.zshrc でzshrcファイルを開く
    • /bin/cat ~/.zshrc で確認もできる)
  2. ":$PATH"の設定を修正
  3. 最後に標準のシステムパスを追記
    export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"
  4. .zshrc の再読み込み
    • source ~/.zshrc

最終的な.zshrcファイル差分

export PATH="△△△/△△△△/△△△△"
.
.
.
export PATH="△△△/△△△△/△△△△:$PATH"
.
.
.
# 標準のシステムパス
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH"

追記

# 現在のログインシェルを確認
$ echo $SHELL

PATH を消すとどうなるか(環境はzshとします)

# bash/zsh でパスを消す
$ unset PATH
# 消えたことが確認できます
$ echo $PATH
>

# lsコマンドなどは使えません
$ ls
> zsh: command not found: ls
# 標準のシステムパスを$PATHに再設定
$ export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"

# 標準のシステムパスが.zshrcに書いてあれば.zshrcの適用でもOK
$ source ~/.zshrc
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?