0
0

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】echoに色をつけてくれる関数

0
Posted at

作成の経緯

bash-itのようなライブラリを入れれば可能とは思うのですが、そこまではしたくない人向けにechoの出力に色をつけてくれる関数を作りました(作ってもらいました)。

コード

echoc() {
    # ヘルプ表示
    if [[ "$1" == "-h" || "$1" == "--help" ]]; then
        cat << 'EOF'
echoc - カラー付きで文字列を出力する関数

使い方:
  echoc <color> <text>

色指定(頭文字1文字):
  r = red
  g = green
  y = yellow
  b = blue
  m = magenta
  c = cyan

例:
  echoc r "エラーが発生しました"
  echoc g "成功しました"
  echoc y "警告: 設定が未入力です"
EOF
        return 0
    fi

    # 通常処理
    local color="$1"
    shift
    local text="$*"

    case "$color" in
        r) code="31" ;; # red
        g) code="32" ;; # green
        y) code="33" ;; # yellow
        b) code="34" ;; # blue
        m) code="35" ;; # magenta
        c) code="36" ;; # cyan
        *) code="0"  ;; # default (no color)
    esac

    echo -e "\033[${code}m${text}\033[0m"
}

出力結果 例

image.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?