源码
#!/bin/bash
# usage:
# echo hhh|mark str [font_color] [background_color]
# font_color and background_color is optional, default is black&white
rp_str=$1
f_color=30
b_color=47
if [ "${f_color}a" == "a" ]; then
f_color=30
fi
if [ "${b_color}a" == "a" ]; then
b_color=47
fi
src_str=""
while read line || [ "${line}" ];
do
src_str="${src_str}${line}\n"
done
echo -e "${src_str}" | awk -v rp_str="${rp_str}" -v f_color="${f_color}" -v b_color="${b_color}" '{gsub(rp_str, "\033["b_color";"f_color"m"rp_str"\033[0m")}1'