I have a shell function that colorizes a filename using tput
.
color=$( tput setaf 1 )
normal=$( tput sgr0 )
colorize() {
echo "${color}$1$(normal)"
}
When I call the function, the terminal displays the colorized filename as expected.
$ myScript.sh .
/path/to/dir # (in color red)
Then, if I redirect output to a file:
$ myScript.sh > /tmp/file.log
file.log
still contains escapes sequences like:
^[[36m~/path/to/my/file/^[(B^[[[m
Could this be related to the TERM and infocomp, and how terminal interprets escape sequences?
The idea is to simulate a "non-display" terminal, isn't it?
My TERM is (Ubuntu 16.04):
$ echo $TERM
xterm
What should I do to prevent such tput
escape sequences when my script is redirected to a file?
Workaround: add an option to my script to disable colorization manually, as in ls
or grep
like --color=none
.
echo
of my caller script. What to do to use this with subsripts ? – el-teedee Feb 11 '18 at 20:17[[ -t 1 ]] && export coloredModed=1
in caller script, andif [[ -t 1 || $coloredMode = 1 ]]
in subscript – el-teedee Feb 11 '18 at 20:24