我正在grep
bash 脚本中运行。如果我grep
直接运行该命令,我会看到文件名和搜索字符串的彩色输出:
grep -i username /tmp/sess_*
如果我将该命令放入 bash 脚本中,它可以工作,但颜色会丢失。如何保留彩色输出?
答案1
你可以添加--color=always
到你的 grep 中。
我本地联机帮助页中的相关部分:
--color[=WHEN], --colour[=WHEN] Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences to display them in color on the terminal. The colors are defined by the environment variable GREP_COLORS. The deprecated environment variable GREP_COLOR is still supported, but its setting does not have priority. WHEN is never, always, or auto.
作为这个问题因此建议,您可能有一个别名将grep
其映射到grep --color=auto
您的文件中.bashrc
,并且脚本不会读取该别名,因此不会在脚本中应用。