echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"
示例
上面的命令在Ubuntu上生成:
前景和背景颜色命令
tput setab [1-7] # Set the background colour using ANSI escapetput setaf [1-7] # Set the foreground colour using ANSI escape
颜色如下:
Num Colour #define R G B
0 black COLOR_BLACK 0,0,01 red COLOR_RED 1,0,02 green COLOR_GREEN 0,1,03 yellow COLOR_YELLOW 1,1,04 blue COLOR_BLUE 0,0,15 magenta COLOR_MAGENTA 1,0,16 cyan COLOR_CYAN 0,1,17 white COLOR_WHITE 1,1,1
tput bold # Select bold modetput dim # Select dim (half-bright) modetput smul # Enable underline modetput rmul # Disable underline modetput rev # Turn on reverse video modetput smso # Enter standout (bold) modetput rmso # Exit standout mode
光标移动命令
tput cup Y X # Move cursor to screen postion X,Y (top left is 0,0)tput cuf N # Move N characters forward (right)tput cub N # Move N characters back (left)tput cuu N # Move N lines uptput ll # Move to last line, first column (if no cup)tput sc # Save the cursor positiontput rc # Restore the cursor positiontput lines # Output the number of lines of the terminaltput cols # Output the number of columns of the terminal
清除和插入命令
tput ech N # Erase N characterstput clear # Clear screen and move the cursor to 0,0tput el 1 # Clear to beginning of linetput el # Clear to end of linetput ed # Clear to end of screentput ich N # Insert N characters (moves rest of line forward!)tput il N # Insert N lines
其他命令
tput sgr0 # Reset text format to the terminal's defaulttput bel # Play a bell
| | bash | hex | octal | NOTE ||-------+-------+---------+---------+------------------------------|| start | \e | \x1b | \033 | || start | \E | \x1B | - | x cannot be capital || end | \e[0m | \x1b[0m | \033[0m | || end | \e[m | \x1b[m | \033[m | 0 is appended if you omit it || | | | | |
简短示例:
| color | bash | hex | octal | NOTE ||-------------+--------------+----------------+----------------+---------------------------------------|| start green | \e[32m<text> | \x1b[32m<text> | \033[32m<text> | m is NOT optional || reset | <text>\e[0m | <text>\1xb[0m | <text>\033[om | o is optional (do it as best practice || | | | | |
function echocolor() { # $1 = stringCOLOR='\033[1;33m'NC='\033[0m'printf "${COLOR}$1${NC}\n"}
echo "This won't be colored"echocolor "This will be colorful"
cecho(){RED="\033[0;31m"GREEN="\033[0;32m" # <-- [0 means not boldYELLOW="\033[1;33m" # <-- [1 means boldCYAN="\033[1;36m"# ... Add more colors if you like
NC="\033[0m" # No Color
# printf "${(P)1}${2} ${NC}\n" # <-- zshprintf "${!1}${2} ${NC}\n" # <-- bash}
┌───────┬─────────────────┬──────────┐ ┌───────┬─────────────────┬──────────┐│ Code │ Style │ Octal │ │ Code │ Style │ Octal │├───────┼─────────────────┼──────────┤ ├───────┼─────────────────┼──────────┤│ - │ Foreground │ \033[3.. │ │ B │ Bold │ \033[1m ││ _ │ Background │ \033[4.. │ │ U │ Underline │ \033[4m │├───────┼─────────────────┼──────────┤ │ F │ Flash/blink │ \033[5m ││ k │ Black │ ......0m │ │ N │ Negative │ \033[7m ││ r │ Red │ ......1m │ ├───────┼─────────────────┼──────────┤│ g │ Green │ ......2m │ │ L │ Normal (unbold) │ \033[22m ││ y │ Yellow │ ......3m │ │ 0 │ Reset │ \033[0m ││ b │ Blue │ ......4m │ └───────┴─────────────────┴──────────┘│ m │ Magenta │ ......5m ││ c │ Cyan │ ......6m ││ w │ White │ ......7m │└───────┴─────────────────┴──────────┘
示例:
echo -e "$(c 0wB)Bold white$(c) and normal"echo -e "Normal text… $(c r_yB)BOLD red text on yellow background… $(c _w)now onwhite background… $(c 0U) reset and underline… $(c) and back to normal."
# ColorRED='\033[0;31m'GREEN='\033[0;32m'YELLOW='\033[0;33m'NC='\033[0m' # No Color
function red {printf "${RED}$@${NC}\n"}
function green {printf "${GREEN}$@${NC}\n"}
function yellow {printf "${YELLOW}$@${NC}\n"}
echo 📕: error messageecho 📙: warning messageecho 📗: ok status messageecho 📘: action messageecho 📔: Or anything you like and want to recognize immediately by colorecho 💩: Or with a specific emoji