LoginSignup
28
12

More than 3 years have passed since last update.

【ターミナル】日本語が文字化けしてしまう時の解決方法(環境変数の設定まで)

Last updated at Posted at 2020-03-27

ある日、desktopでls実行した時に…

ターミナル
lancai@oja Desktop % ls -l
total 23336
drwxr-xr-x@ 27 lancai  staff      864 Feb 29 14:14 ajax_crud_sample
drwxr-xr-x   4 lancai  staff      128 Feb 27 23:57 git_sample
drwxr-xr-x   4 lancai  staff      128 Feb 27 23:05 hell
drwxr-xr-x@  4 lancai  staff      128 Mar  1 13:25 hello
drwxr-xr-x   3 lancai  staff       96 Feb 29 14:08 helloHeroku
drwxr-xr-x  13 lancai  staff      416 Mar 12 22:27 joint_dev_task
drwxr-xr-x   5 lancai  staff      160 Mar 21 16:15 mosha
drwxr-xr-x@  9 lancai  staff      288 Mar 26 22:19 npm_sample
-rw-r--r--@  1 lancai  staff  4634176 Mar 26 22:23 ??????????????????????????? 2020-03-26 22.23.14.png
-rw-r--r--@  1 lancai  staff  2202423 Mar 27 21:08 ??????????????????????????? 2020-03-27 21.07.57.png
-rw-r--r--@  1 lancai  staff  5104788 Mar 27 21:58 ??????????????????????????? 2020-03-27 21.58.42.png

下3行のファイル名が文字化けしてて「😶!?」

そういえばRubyか何かの環境構築する時に日本語打ったら文字化けしてたけど解決せずそのままだったなあ…と!!

ということでこれを機に色々ググった結果、解決したのでその方法を記録していきます。

ターミナルの設定

ターミナル>環境設定>プロファイル>詳細

ターミナル設定.png
↑赤色の丸印2箇所が画像の通りになっているかチェック

ターミナル>環境設定>エンコーディング

ターミナル設定2.png
↑画像の通りにUnicode(UTF-8)にチェック

LANGの値を確認

ご自身のホームディレクトリにて以下を実行
※ホームディレクトリ・・・ターミナルでcdを実行した時に移動できる。

ターミナル
lancai@oja % echo $LANG
C

※↑ここでja_JP.UTF-8と出ていたら解決とのことだが、Cと表示される。

現在使っているシェルを表示

lancai@oja ~ % echo $SHELL
/bin/zsh
  • /bin/zshが表示されたら~/.zshenv
  • /bin/bashが表示されたら~/.bash_profile

のいずれかのファイル内にexport LANG=ja_JP.UTF-8を追記する。(=環境変数の設定が完了)

私の場合は/bin/zshが表示されたので~/.zshenvexport LANG=ja_JP.UTF-8を追記することで
環境変数の設定が完了するということです。

~/.zshenv~/.bash_profileの場所

ホームディレクトリ内にあるらしい

ホームディレクトリに移動

ターミナル
lancai@oja desktop % cd
lancai@oja ~ %

ホームディレクトリ内にあるファイルなどを表示

lancai@oja ~ % ls -a
.           .nodebrew       Library
..          .npm            Movies
.CFUserTextEncoding .oracle_jre_usage   Music
.DS_Store       .rbenv          MyVagrant
.Trash          .ssh            Pictures
.atom           .subversion     Public
.bash_profile       .vagrant.d      VirtualBox VMs
.bash_profile.swp   .viminfo        afax_crud_sample
.bash_profile.txt   .vscode         agametter_app
.bashrc         .yarnrc         ajax_crud_sample
.bundle         .zsh_history        demo
.config         .zshrc          dq_like_app
.gem            .zshrc.swp      es6_sample
.gitconfig      Applications        gitwork
.inputrc.txt        Desktop         hello
.local          Documents
.netrc          Downloads

本来上記の中に入ってるらしいのですが
どうやら私の場合、~/.zshenvが無かったようなので
以下の方法で作りました。

touchでテキストファイル作成

ターミナル
lancai@oja ~ % touch ~/.zshenv

↓作成できたはずなのでopenで開く(テキストエディタが開きます)

lancai@oja ~ % open ~/.zshenv

テキストエディタ内にexport LANG=ja_JP.UTF-8を入力し、保存
テキストエディタ.png

ターミナルに戻り、以下を実行

lancai@oja ~ % exec $SHELL -l

LANGの値を確認

lancai@oja ~ % echo $LANG
ja_JP.UTF-8

ja_JP.UTF-8に設定できたみたいなのでdesktopでls実行

lancai@oja ~ % cd desktop
lancai@oja desktop % ls
ajax_crud_sample
git_sample
hell
hello
helloHeroku
joint_dev_task
mosha
npm_sample
スクリーンショット 2020-03-26 22.23.14.png
スクリーンショット 2020-03-27 21.07.57.png
スクリーンショット 2020-03-27 21.58.42.png

日本語の文字化け部分が解消されました👍

28
12
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
28
12