@programming
投稿日 2024/10/21
更新日 2024/10/21 ✏
bash: Linuxターミナルの文字や背景に色付け
ターミナル出力で文字や背景に色を付ける
色コード一覧:
FG BG Name
30 40 Black
31 41 Red
32 42 Green
33 43 Yellow
34 44 Blue
35 45 Magenta
36 46 Cyan
37 47 White
90 100 Bright Black (Gray)
91 101 Bright Red
92 102 Bright Green
93 103 Bright Yellow
94 104 Bright Blue
95 105 Bright Magenta
96 106 Bright Cyan
97 107 Bright White
39 49 No color (Reset)
テストしてみる:
## fg green 32(fg green) 39(fg reset)
printf "\033[32m fg green. \033[39m"
## fg green / bg white
## 32(fg green) 107(bg white) 49(bg reset) 39(fg reset)
printf "\033[32m \033[107m fg green / bg white. \033[49m \033[39m"
bashスクリプトで使えるスニペットコード
#!/usr/bin/env bash
# Reset
NC='\033[0m' # Text Reset
# Regular Colors
BLACK='\033[0;30m' # BLACK
RED='\033[0;31m' # RED
GREEN='\033[0;32m' # GREEN
YELLOW='\033[0;33m' # YELLOW
BLUE='\033[0;34m' # BLUE
PURPLE='\033[0;35m' # PURPLE
CYAN='\033[0;36m' # CYAN
WHITE='\033[0;37m' # WHITE
# Bold
BBLACK='\033[1;30m' # BLACK
BRED='\033[1;31m' # RED
BGREEN='\033[1;32m' # GREEN
BYELLOW='\033[1;33m' # YELLOW
BBLUE='\033[1;34m' # BLUE
BPURPLE='\033[1;35m' # PURPLE
BCYAN='\033[1;36m' # CYAN
BWHITE='\033[1;37m' # WHITE
# Underline
UBLACK='\033[4;30m' # BLACK
URED='\033[4;31m' # RED
UGREEN='\033[4;32m' # GREEN
UYELLOW='\033[4;33m' # YELLOW
UBLUE='\033[4;34m' # BLUE
UPURPLE='\033[4;35m' # PURPLE
UCYAN='\033[4;36m' # CYAN
UWHITE='\033[4;37m' # WHITE
# Background
BGBLACK='\033[40m' # BLACK
BGRED='\033[41m' # RED
BGGREEN='\033[42m' # GREEN
BGYELLOW='\033[43m' # YELLOW
BGBLUE='\033[44m' # BLUE
BGPURPLE='\033[45m' # PURPLE
BGCYAN='\033[46m' # CYAN
BGWHITE='\033[47m' # WHITE
# High Intensity
HIBLACK='\033[0;90m' # BLACK
HIRED='\033[0;91m' # RED
HIGREEN='\033[0;92m' # GREEN
HIYELLOW='\033[0;93m' # YELLOW
HIBLUE='\033[0;94m' # BLUE
HIPURPLE='\033[0;95m' # PURPLE
HICYAN='\033[0;96m' # CYAN
HIWHITE='\033[0;97m' # WHITE
# Bold High Intensity
BIBLACK='\033[1;90m' # BLACK
BIRED='\033[1;91m' # RED
BIGREEN='\033[1;92m' # GREEN
BIYELLOW='\033[1;93m' # YELLOW
BIBLUE='\033[1;94m' # BLUE
BIPURPLE='\033[1;95m' # PURPLE
BICYAN='\033[1;96m' # CYAN
BIWHITE='\033[1;97m' # WHITE
# High Intensity backgrounds
BGHIBLACK='\033[0;100m' # BLACK
BGHIRED='\033[0;101m' # RED
BGHIGREEN='\033[0;102m' # GREEN
BGHIYELLOW='\033[0;103m' # YELLOW
BGHIBLUE='\033[0;104m' # BLUE
BGHIPURPLE='\033[0;105m' # PURPLE
BGHICYAN='\033[0;106m' # CYAN
BGHIWHITE='\033[0;107m' # WHITE
## 使い方:
echo -e "${CYAN}This color is cyan!${NC}"
参考:
おまけ: JS版標準出力(またはコンソールログ)に色付け
console.log('\x1b[36m%s\x1b[0m', 'I am cyan'); //cyan
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow); //yellow
Colors reference:
Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"
FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"
FgGray = "\x1b[90m"
BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"
BgGray = "\x1b[100m"
参考:How to change node.js's console font color? - stackoverflow
以上