ターミナルの文字に色を付ける設定は、通常、~/.bashrc
の以下の行に書かれています。
以下の例は、AWS EC2のUbuntu18.04の~/.bashrc
からの抜粋しました。
# uncomment for a colored prompt, if the terminal has the capability; turned # off by default to not distract the user: the focus in a terminal window # should be on the output of commands, not on the prompt # force_color_prompt=yes if [ -n "$force_color_prompt" ]; then if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then # We have color support; assume it's compliant with Ecma-48 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such # a case would tend to support setf rather than setaf.) color_prompt=yes else color_prompt= fi fi if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi unset color_prompt force_color_prompt
このような記述があればターミナルの文字に色が付きます。
しかし、以前、仕事中に突然ターミナルの色が消えてしまう(白黒になる)ことがありました。
~/.bashrc
はターミナル起動後に毎回実行されるはずなので、再起動したら直るかと思いましたが、無駄でした。
原因を探っていくと、実は「~/.bashrc
はターミナル起動後に毎回実行されるわけではない」ということがわかりました。
~/.bashrc
が実行されるかどうかは、~/.profile
の中に書かれています。
~/.profile
の中を見てみましょう。
# ~/.profile: executed by the command interpreter for login shells. # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login # exists. # see /usr/share/doc/bash/examples/startup-files for examples. # the files are located in the bash-doc package. # the default umask is set in /etc/profile; for setting the umask # for ssh logins, install and configure the libpam-umask package. #umask 022 # if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi # set PATH so it includes user's private bin if it exists if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi # set PATH so it includes user's private bin if it exists if [ -d "$HOME/.local/bin" ] ; then PATH="$HOME/.local/bin:$PATH" fi
これを見てみると、中央あたりで.bashrc
が実行されていることがわかります。また、1行目のコメントを見てみると、
~/.bash_profile
や~/.bash_login
というファイルが存在する場合、.profile
は読み込まれないと書かれています。
原因はこれでした!
ホームディレクトリを見てみると、~/.bash_profile
が存在しました。どうやら、マシンの環境設定中に、本来は~/.bashrc
を編集すべきところ誤って~/.bash_profile
を作ってしまったのだと思います。~/.bash_profile
を削除したら無事、ターミナルが色を取り戻しました。
※ ~/.bash_profile
には重要な情報が書かれていることがあります。~/.bash_profile
を削除して何らかの被害が生じても、本記事および本記事の執筆者は責任を負えません。ご自身の判断と責任のもとで削除してください。
以上、ターミナルの色が付かない問題の解決方法でした。