LoginSignup
0
0

bashのカラー文字出力について

Posted at

bashで文字列に色属性をつけて出力するサンプルです。

サンプルプログラム

color.sh
#!/bin/bash

# set -n
# set -x
  set -o ignoreeof
  set +m
  set -e
  set -u

  trap 'exit 1' 1 2 3 15

# -----------------------------------------------------------------------------
  readonly TXT_RESET='\033[m'           # reset all attributes
  readonly TXT_ULINE='\033[4m'          # set underline
  readonly TXT_ULINERST='\033[24m'      # reset underline
  readonly TXT_REV='\033[7m'            # set reverse display
  readonly TXT_REVRST='\033[27m'        # reset reverse display
  readonly TXT_BLACK='\033[30m'         # text black
  readonly TXT_RED='\033[31m'           # text red
  readonly TXT_GREEN='\033[32m'         # text green
  readonly TXT_YELLOW='\033[33m'        # text yellow
  readonly TXT_BLUE='\033[34m'          # text blue
  readonly TXT_MAGENTA='\033[35m'       # text purple
  readonly TXT_CYAN='\033[36m'          # text light blue
  readonly TXT_WHITE='\033[37m'         # text white
  readonly TXT_BBLACK='\033[40m'        # text reverse black
  readonly TXT_BRED='\033[41m'          # text reverse red
  readonly TXT_BGREEN='\033[42m'        # text reverse green
  readonly TXT_BYELLOW='\033[43m'       # text reverse yellow
  readonly TXT_BBLUE='\033[44m'         # text reverse blue
  readonly TXT_BMAGENTA='\033[45m'      # text reverse purple
  readonly TXT_BCYAN='\033[46m'         # text reverse light blue
  readonly TXT_BWHITE='\033[47m'        # text reverse white

# --- text color test ---------------------------------------------------------
funcColorTest () {
  echo -e "${TXT_RESET} : TXT_RESET    : ${TXT_RESET}"
  echo -e "${TXT_ULINE} : TXT_ULINE    : ${TXT_RESET}"
  echo -e "${TXT_ULINERST} : TXT_ULINERST : ${TXT_RESET}"
  echo -e "${TXT_REV} : TXT_REV      : ${TXT_RESET}"
  echo -e "${TXT_REVRST} : TXT_REVRST   : ${TXT_RESET}"
  echo -e "${TXT_BLACK} : TXT_BLACK    : ${TXT_RESET}"
  echo -e "${TXT_RED} : TXT_RED      : ${TXT_RESET}"
  echo -e "${TXT_GREEN} : TXT_GREEN    : ${TXT_RESET}"
  echo -e "${TXT_YELLOW} : TXT_YELLOW   : ${TXT_RESET}"
  echo -e "${TXT_BLUE} : TXT_BLUE     : ${TXT_RESET}"
  echo -e "${TXT_MAGENTA} : TXT_MAGENTA  : ${TXT_RESET}"
  echo -e "${TXT_CYAN} : TXT_CYAN     : ${TXT_RESET}"
  echo -e "${TXT_WHITE} : TXT_WHITE    : ${TXT_RESET}"
  echo -e "${TXT_BBLACK} : TXT_BBLACK   : ${TXT_RESET}"
  echo -e "${TXT_BRED} : TXT_BRED     : ${TXT_RESET}"
  echo -e "${TXT_BGREEN} : TXT_BGREEN   : ${TXT_RESET}"
  echo -e "${TXT_BYELLOW} : TXT_BYELLOW  : ${TXT_RESET}"
  echo -e "${TXT_BBLUE} : TXT_BBLUE    : ${TXT_RESET}"
  echo -e "${TXT_BMAGENTA} : TXT_BMAGENTA : ${TXT_RESET}"
  echo -e "${TXT_BCYAN} : TXT_BCYAN    : ${TXT_RESET}"
  echo -e "${TXT_BWHITE} : TXT_BWHITE   : ${TXT_RESET}"
}

# --- main --------------------------------------------------------------------
  funcColorTest
  exit 0

実行結果

color.png

使用例

実行例

sample.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