Grep usually shows highlighted text in red which is what I wanted but this time it doesn't seem to do that no matter what I do. I am doing this from a Docker container (nginx:1.19-alpine more specifically) and I installed Bash 5.1 using RUN apk add bash. Then I added a ~/.bashrc config file with this contents:
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
force_color_prompt=yes
color_prompt=yes
if [ "$color_prompt" = yes ]; then
PS1='๐ณ ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@server-nginx\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='๐ณ ${debian_chroot:+($debian_chroot)}\u@server-nginx:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@server-nginx: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
The thing is that $ ls does show colored output. But when I try to pipe it through grep it still is black and white.
// Directories have colors!!
$ ls
// No color highlighting here
$ echo "baba" | grep --color=always ba
// No colors here either
$ ls | grep bin
The exact same setup on a different Docker image worked perfectly (php:7.4-fpm and node:14) so maybe there is something specific about the nginx:1.19-alpine image that it doesn't show colors for grep?
P.S. printf '%q\n' "$GREP_COLORS" "$GREP_COLOR" prints this output:
$ printf '%q\n' "$GREP_COLORS" "$GREP_COLOR"
ms=01\;33
''
After much digging around, I finally found the solution. The version of grep that came with this particular Docker image (nginx:1.19-alpine) was some kind of a "busybox version of grep". Anyway, upgrading grep did the trick:
// Inside of Dockerfile added this
RUN apk add --no-cache --upgrade grep
Now grep shows colored output. Phew! Hope this helps others who might run into this same issue.