Bashでコマンドプロンプトの色を変更するには? [コピー]

Bashでコマンドプロンプトの色を変更するには? [コピー]

重複の可能性:
コマンドプロンプトを設定するために.bashrcをカスタマイズする方法は?

コマンドを実行すると、コマンド出力の先頭を見つけるのが難しいことがよくあります。この問題を解決する簡単な方法は、コマンドプロンプトで色を付けるか太字で表示して、中断した場所を簡単に確認できるようにすることです。どのように?

ベストアンサー1

これが私がすることです。 Escapeステートメントは人間が読みにくいので、追加のEscapeステートメントの代わりにtput(1)を使用します。

これは私の.bashrcからのものです。

### Set the prompt like "username@hostname:~ $"
# See: http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/
# And: http://mywiki.wooledge.org/BashFAQ/037
# 'tput bold' will work regardless of the foreground and background colors.
# Place the tput output into variables, so they are only execd once.
bold=$(tput bold)
reset=$(tput sgr0)
export PS1="\u@\[$bold\]\h\[$reset\]:\w \$ "

これは別のオプションです。これはエスケープシーケンスよりも読みやすくなります。

# Bash
red=$(tput setaf 1)
green=$(tput setaf 2)
blue=$(tput setaf 4)
reset=$(tput sgr0)
PS1='\[$red\]\u\[$reset\]@\[$green\]\h\[$reset\]:\[$blue\]\w\[$reset\]\$ '

おすすめ記事