233

I have some script that produces output with colors and I need to remove the ANSI codes.

#!/bin/bash

exec > >(tee log)   # redirect the output to a file but keep it on stdout
exec 2>&1

./somescript

The output is (in log file):

java (pid  12321) is running...@[60G[@[0;32m  OK  @[0;39m]

I didn't know how to put the ESC character here, so I put @ in its place.

I changed the script into:

#!/bin/bash

exec > >(tee log)   # redirect the output to a file but keep it on stdout
exec 2>&1

./somescript | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

But now it gives me (in log file):

java (pid  12321) is running...@[60G[  OK  ]

How can I also remove this '@[60G?

Maybe there is a way to completely disable coloring for the entire script?

mowzy
  • 105
  • 1
  • 8
Pawel P.
  • 3,731
  • 4
  • 20
  • 20
  • 2
    For node/npm, you can use `strip-ansi`: https://github.com/chalk/strip-ansi. – Joshua Pinter Jun 09 '20 at 19:12
  • This was previously asked and answered: https://stackoverflow.com/questions/6534556/how-to-remove-and-all-of-the-escape-sequences-in-a-file-using-linux-shell-sc/6534712#6534712 – mike Oct 22 '22 at 23:07

21 Answers21

243

According to Wikipedia, the [m|K] in the sed command you're using is specifically designed to handle m (the color command) and K (the "erase part of line" command). Your script is trying to set absolute cursor position to 60 (^[[60G) to get all the OKs in a line, which your sed line doesn't cover.

(Properly, [m|K] should probably be (m|K) or [mK], because you're not trying to match a pipe character. But that's not important right now.)

If you switch that final match in your command to [mGK] or (m|G|K), you should be able to catch that extra control sequence.

./somescript | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g"
S Anand
  • 11,364
  • 2
  • 28
  • 23
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • 45
    BSD/OSX users: We usually don't have the -r option to sed. `brew install gnu-sed` will install a capable version. Run with `gsed`. – Nicolai S Jul 06 '15 at 02:07
  • 1
    If I do `echo "$(tput setaf 1)foo$(tput sgr0) bar" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" | cat -A` , I get : `foo^O bar$` So I guess some characters are not correctly removed, right ? Do you know how to correct ? – edi9999 Feb 23 '16 at 11:29
  • 2
    @edi9999 As far as I can tell, the difference there is that color settings beyond 16 colors (as `setaf` supports) require more parameters than just two; my regex supports two. Changing the first `?` out for `*` should help. Handling `sgr0` is possible but based on a search it likely grows outside the scope of this hacky regex-based answer. – Jeff Bowman Feb 23 '16 at 18:37
  • Ok, I've added an answer which adds a `sed` to the pipe to strip the "shift in" character – edi9999 Feb 24 '16 at 08:07
  • 8
    This doesn't work reliably as there can be a third value (ala `[38;5;45m`). This alternative answer works https://unix.stackexchange.com/a/55547/168277 – davemyron Jul 05 '17 at 19:16
  • What about removing output formatting control characters ? For example, running `maestro-ng status > file.txt` will have a lot of styling special characters. Do you have an idea? – Ahmed Hussein Feb 22 '20 at 18:29
  • Using the script I wasn't able to remove the coloring in here `\u001b[0;1;34mVARIABLE\u001b[0;m"` – alper Jul 03 '20 at 21:29
  • This does not work for me. It is showing as a different color of grey. – Clay Risser Sep 04 '21 at 14:36
  • 4
    Adding this as `alias decolorize='sed -r "s/\\x1B\\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g"'` to your bashrc creates a very nice utility that can be used both as `command | decolorize` and as `decolorize file.log`. – Neinstein Sep 06 '21 at 12:17
  • 7
    More correct form is `sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,3})*)?[mGK]//g"`, because there can be more terms and they can be also 3 digits long. E.g. `\e[38;5;123m` or even `\e[38;5;123;48;5;246m`. – FERcsI Jan 25 '22 at 05:07
  • For @FERCSl's update, note both `{1,3}` in both expressions, and `*` instead of `?` for the inner group. Valid coloring: `echo -e '\x1b[38;5;246;03mcolor me timbers'` matched by @FERcsl, but not original. – mcint Feb 12 '23 at 21:07
83

IMHO, most of these answers try too hard to restrict what is inside the escape code. As a result, they end up missing common codes like [38;5;60m (foreground ANSI color 60 from 256-color mode).

They also require the -r option which enables GNU extensions. These are not required; they just make the regex read better.

Here is a simpler answer that handles the 256-color escapes and works on systems with non-GNU sed:

./somescript | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g'

This will catch anything that starts with [, has any number of decimals and semicolons, and ends with a letter. This should catch any of the common ANSI escape sequences.

For funsies, here's a larger and more general (but minimally tested) solution for all conceivable ANSI escape sequences:

./somescript | sed 's/\x1B[@A-Z\\\]^_]\|\x1B\[[0-9:;<=>?]*[-!"#$%&'"'"'()*+,.\/]*[][\\@A-Z^_`a-z{|}~]//g'

(and if you have @edi9999's SI problem, add | sed "s/\x0f//g" to the end; this works for any control char by replacing 0f with the hex of the undesired char)

meustrus
  • 6,637
  • 5
  • 42
  • 53
  • 1
    This one worked nicely to string colour out of Azure az cli prettified output. – volvox Oct 04 '19 at 13:56
  • Fixed @elig. Turns out it had a number of issues, starting with some editor replacing all my dashes with weird unicode versions, but also a bunch of improper escaping - `|` in sed, `]` inside a character class in sed, and `'` in a single-quoted bash string. It is now working for me for a very basic test case. – meustrus Mar 23 '20 at 13:46
  • I think there might be an error with the first regex - `\+` will make the plus sign a literal, but I think it is mean to be an "at least one" modifier of the previous range. – halfer Aug 11 '20 at 20:20
  • 1
    @halfer, when using `sed` without the `-r` option, `+` is treated as a literal and `\+` is treated as a modifier, in contradiction to most modern usage. – meustrus Aug 31 '20 at 20:18
  • @meustrus: ah right, thanks. Out of interest, is this behaviour at the shell level or at the Sed level? It sounds like you're describing something in Sed, which I agree is an unexpected syntax! – halfer Aug 31 '20 at 20:23
  • So actually, I was wrong. I think `\+` worked on Mac, but it's not part of [the POSIX specification](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html#tag_20_116_13_02). In the interest of maximum correctness and portability, I've changed `\+` to `\{1,\}`. To answer your new question, this behavior is specific to `sed`, not your shell. – meustrus Aug 31 '20 at 20:27
  • These work on macos if you use `gsed` instead of `sed`. – slm Apr 29 '21 at 18:02
  • 2
    This is my preferred answer, but for my use case there was a small problem, the output I was processing contained `^[[m` which was not being caught. Resolved by amending like so `./somescript | sed 's/\x1B\[[0-9;]*[A-Za-z]//g'` – bxm Aug 29 '21 at 10:10
  • This solution did not work for me. It left some escape codes that weren't visible from the terminal inside a text file. (generated by a `git diff`) The accepted answer worked. – KernelDeimos Feb 03 '22 at 22:34
59

I came across ansi2txt tool from colorized-logs package in Debian. The tool drops ANSI control codes from STDIN.

Usage example:

./somescript | ansi2txt

Source code http://github.com/kilobyte/colorized-logs

user4674453
  • 590
  • 1
  • 9
  • 12
  • 5
    The package 'colorized-logs' exists in Ubuntu's standard repository, which makes the installation of this tool very easy with: 'sudo apt install colorized-logs'. It works great and I have yet to find a problem with it. This should be the official answer. – Hans Deragon Jun 15 '22 at 15:22
  • 1
    Likewise for AUR folks: aur/colorized-logs – draxil Feb 01 '23 at 15:37
  • From the manual page: All ANSI codes are simply ignored, including all cursor positioning ones. This package removes all colorized parts, e.g. timestamp which makes it pretty useless. – Roman Shishkin May 22 '23 at 14:56
48

I couldn't get decent results from any of the other answers, but the following worked for me:

somescript | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g"

If I only removed the control char "^[", it left the rest of the color data, e.g., "33m". Including the color code and "m" did the trick. I'm puzzled with s/\x1B//g doesn't work because \x1B[31m certainly works with echo.

JoeAndrieu
  • 804
  • 6
  • 6
  • 7
    On OSX (BSD sed), use `-E` instead of `-r` for extended regex. More could be found [here](https://unix.stackexchange.com/a/131940) – Assambar Jan 08 '18 at 13:51
  • i had to replace `{1,3}` to `{,3}` (otherwise it was still skipping some controls), thanks for your solution! – actionless Apr 17 '18 at 19:28
  • 8
    Since they might be multiple numbers separated with semi-colons (for background color, bold, italic, etc...). This command worked for me: `sed -r "s/[[:cntrl:]]\[([0-9]{1,3};)*[0-9]{1,3}m//g"` – saeedgnu Jul 27 '18 at 03:22
  • This one (of the many I tested) worked with Ansible output that had been run with unbuffer. – Martin Nov 08 '18 at 20:39
  • for those that want to view logs that contain color codes using the less command this worked for me on ubuntu. `cat errors.log | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g" | tee errors-copy.log | less errors-copy.log` – SlurpGoose Nov 09 '20 at 21:33
  • This does not catch all escape codes for me unlike the excepted answer. – SuperSandro2000 Dec 31 '20 at 07:05
45

For Mac OSX or BSD use

./somescript | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g'
grebulon
  • 7,697
  • 5
  • 42
  • 66
31

The regular expression below will miss some ANSI Escape Codes sequences, as well as 3 digit colors. Example and Fix on regex101.com.

Use this instead:

./somescript | sed -r 's/\x1B\[(;?[0-9]{1,3})+[mGK]//g'

I also had the problem that sometimes, the SI character appeared.

It happened for example with this input : echo "$(tput setaf 1)foo$(tput sgr0) bar"

Here's a way to also strip the SI character (shift in) (0x0f)

./somescript | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" | sed "s/\x0f//g"
SgtPooki
  • 11,012
  • 5
  • 37
  • 46
edi9999
  • 19,701
  • 13
  • 88
  • 127
  • 2
    Not sure why this answer receives so little credit. This is the only one working for me... – m8mble Mar 08 '17 at 15:04
  • 1
    This one is close to working, but it misses the three digit case and sequences of color codes like: `U+001B[38;2;128;128;128m`. See the unfound colors at https://regex101.com/r/Qjtopi/1. Regex that works for me can be found at https://regex101.com/r/wYygBw/1 – SgtPooki Feb 24 '21 at 20:30
11

Much simpler function in pure Bash to filter-out common ANSI codes from a text stream:

# Strips common ANSI codes from a text stream

shopt -s extglob # Enable Bash Extended Globbing expressions
ansi_filter() {
  local line
  local IFS=
  while read -r line || [[ "$line" ]]; do
    printf '%s\n' "${line//$'\e'[\[(]*([0-9;])[@-n]/}"
  done
}

See:

  1. linuxjournal.com: Extended Globbing
  2. gnu.org: Bash Parameter Expansion
Léa Gris
  • 17,497
  • 4
  • 32
  • 41
  • 1
    This doesn’t work. Test with `tldr`. (Though I use zsh so it might also be because of that.) – HappyFace Jul 28 '19 at 07:06
  • Indeed, Zsh will not understand Bash’s extended globing `extglob` or probably neither will it understand string replacement altogether. – Léa Gris Jul 28 '19 at 10:46
  • I did enable the extendedglob of zsh ... String replacement should be posix, too? – HappyFace Jul 28 '19 at 10:48
  • String replacement is not POSIX. You can use any of the alternate methods using `sed` mentioned here that will work with Zsh. – Léa Gris Jul 28 '19 at 11:21
  • 1
    This solution has the advantage of line-buffering the text. I tried with sed but it was block-buffering my pipe. – Guillermo Prandi Aug 11 '19 at 18:33
  • The fundamental idea here, doing simply string replacements, worked well for me: https://stackoverflow.com/a/13210909/565877 I had very specific codes to remove (red, green, stop color), and I have an ugly 6-8 lines removing each color code. I'm sure this could be a fine solution for some use cases, if you made the regex simpler. (Probably the `$'\e'` bit is particularly problematic?) – Devin Rhode Oct 10 '21 at 20:54
9

I had a similar problem. All solutions I found did work well for the color codes but did not remove the characters added by "$(tput sgr0)" (resetting attributes).

Taking, for example, the solution in the comment by davemyron the length of the resulting string in the example below is 9, not 6:

#!/usr/bin/env bash

string="$(tput setaf 9)foobar$(tput sgr0)"
string_sed="$( sed -r "s/\x1B\[[0-9;]*[JKmsu]//g" <<< "${string}" )"
echo ${#string_sed}

In order to work properly, the regex had to be extend to also match the sequence added by sgr0 ("\E(B"):

string_sed="$( sed -r "s/\x1B(\[[0-9;]*[JKmsu]|\(B)//g" <<< "${string}" )"
Jarodiv
  • 136
  • 1
  • 3
  • @Jarodiv - thanks for the most comprehansive approach. All the answers provided on this topic deal ONLY with ANSI/VT100 Control sequences (ex: "\e[31mHello World\e[0m"), however do not remediate anything caused by TPUT text formatting (ex: tput smso/tput setaf X/tput rmso/tput sgr0). As a result after all 'sed' executions there was some other mess remaining in the logs. This is a pure solution to my usecases! – faceless Jun 06 '19 at 13:22
8

There's also a dedicated tool to handle ANSI escape sequences: ansifilter. Use the default --text output format to strip all ANSI escape sequences (note: not just coloring).

ref: https://stackoverflow.com/a/6534712

Juan
  • 1,204
  • 1
  • 11
  • 25
8

Not sure what's in ./somescript but if escape sequences are not hardcoded you can set the terminal type to avoid them

TERM=dumb ./somescript 

For example, if you try

TERM=dumb tput sgr0 | xxd

you'll see it produces no output while

tput sgr0 | xxd
00000000: 1b28 421b 5b6d                           .(B.[m

does (for xterm-256color).

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • 1
    Simplest solution/answer by far! – EdwardTeach Dec 02 '21 at 17:37
  • Assuming your tools pay attention to the terminal type. In general, well behaved tools already have a switch to disable ANSI sequences (usually badly named like `--no-color`) – sehe Oct 23 '22 at 10:35
  • @sehe that's why I mentioned "but if escape sequences are not hardcoded..." – Diego Torres Milano Oct 23 '22 at 22:42
  • I upvoted, but in the general case this is not easy to tell. I suppose it will be a minority of tools that use `tput`, and a somehwat wider set of tools that tries to be smart (or even sometimes correct) about terminal capabilities. – sehe Oct 24 '22 at 21:48
  • `TERM=dumb` folks – fabrizioM Mar 12 '23 at 17:00
4

Here's a pure Bash solution.

Save as strip-escape-codes.sh, make executable and then run <command-producing-colorful-output> | ./strip-escape-codes.sh.

Note that this strips all ANSI escape codes/sequences. If you want to strip colors only, replace [a-zA-Z] with "m".

Bash >= 4.0:

#!/usr/bin/env bash

# Strip ANSI escape codes/sequences [$1: input string, $2: target variable]
function strip_escape_codes() {
    local _input="$1" _i _char _escape=0
    local -n _output="$2"; _output=""
    for (( _i=0; _i < ${#_input}; _i++ )); do
        _char="${_input:_i:1}"
        if (( ${_escape} == 1 )); then
            if [[ "${_char}" == [a-zA-Z] ]]; then
                _escape=0
            fi
            continue
        fi
        if [[ "${_char}" == $'\e' ]]; then
            _escape=1
            continue
        fi
        _output+="${_char}"
    done
}

while read -r line; do
    strip_escape_codes "${line}" line_stripped
    echo "${line_stripped}"
done

Bash < 4.0:

#!/usr/bin/env bash

# Strip ANSI escape codes/sequences [$1: input string, $2: target variable]
function strip_escape_codes() {
    local input="${1//\"/\\\"}" output="" i char escape=0
    for (( i=0; i < ${#input}; ++i )); do         # process all characters of input string
        char="${input:i:1}"                       # get current character from input string
        if (( ${escape} == 1 )); then             # if we're currently within an escape sequence, check if
            if [[ "${char}" == [a-zA-Z] ]]; then  # end is reached, i.e. if current character is a letter
                escape=0                          # end reached, we're no longer within an escape sequence
            fi
            continue                              # skip current character, i.e. do not add to ouput
        fi
        if [[ "${char}" == $'\e' ]]; then         # if current character is '\e', we've reached the start
            escape=1                              # of an escape sequence -> set flag
            continue                              # skip current character, i.e. do not add to ouput
        fi
        output+="${char}"                         # add current character to output
    done
    eval "$2=\"${output}\""                       # assign output to target variable
}

while read -r line; do
    strip_escape_codes "${line}" line_stripped
    echo "${line_stripped}"
done
Fonic
  • 2,625
  • 23
  • 20
3

Hmm, not sure if this will work for you, but 'tr' will 'strip' (delete) control codes - try:

./somescript | tr -d '[:cntrl:]'
slm
  • 15,396
  • 12
  • 109
  • 124
Dale_Reagan
  • 1,953
  • 14
  • 11
  • 46
    Suddenly it's also remove new lines – ruX Nov 28 '14 at 16:48
  • 1
    Yes, LF and CR (codes) are control codes; if your are interested in more than one line then this may not be a solution. Since it appears that you are running a JAVA program I will guess that the colors are managed from there; Otherwise you would need to look at your console setup (i.e. terminal settings/color scheme) and/or at the options for each command that supports 'colors', i.e. ls --color=never – Dale_Reagan Dec 02 '14 at 01:43
  • 4
    I like this answer for its elegance, even if it does more than just removing colours. Thanks! – Johann Philipp Strathausen Sep 05 '17 at 08:35
  • 10
    it actually let codes there, see ls -l + your command: `rwxr-xr-x 1 tokra admin 22 Oct 18 14:21 [0m[01;36m/usr/local/opt/gradle[0m -> [01;34m../Cellar/gradle/4.2.1[0m/` – To Kra Nov 17 '17 at 20:37
  • 4
    Control codes are not ANSI codes. This does not answer the question at all. – itaych Mar 15 '21 at 14:02
  • 1
    Hmm... You are correct - my response did not directly answer the question - about removing colors. Also, seems that we have read different documents on ANSI codes - the ones I have read include 'control codes' as part of the standards (there are many...) :) – Dale_Reagan Mar 16 '21 at 19:23
3

@jeff-bowman's solution helped me getting rid of SOME of the color codes. I added another small portion to the regex in order to remove some more:

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" # Original. Removed Red ([31;40m[1m[error][0m)
sed -r "s/\x1B\[([0-9];)?([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" # With an addition, removed yellow and green ([1;33;40m[1m[warning][0m and [1;32;40m[1m[ok][0m)
                ^^^^^^^^^
                remove Yellow and Green (and maybe more colors)
zstolar
  • 461
  • 1
  • 4
  • 9
3

The controversial idea would be to reconfigure terminal settings for this process environment to let the process know that terminal does not support colors.

Something like TERM=xterm-mono ./somescript comes to my mind. YMMV with your specific OS and ability of your script to understand terminal color settings.

A B
  • 2,013
  • 2
  • 21
  • 22
  • 1
    I used your solution and it worked. However, I believe you may need to set if to something other than `xterm*`, at least it didn't work for me until I set `TERM` to anything that didn't start with `xterm`. In my case: `TERM= ./my_script` worked like a charm. – David Rissato Cruz Dec 10 '20 at 00:48
2

I had some issues with colorized output which the other solutions here didn't process correctly, so I built this perl one liner. It looks for escape \e followed by opening bracket \[ followed by one or color codes \d+ separated by semicolons, ending on m.

perl -ple 's/\e\[\d+(;\d+)*m//g'

It seems to work really well for colorized compiler output.

CodeMonkey
  • 4,067
  • 1
  • 31
  • 43
1

The other answers didn't quite manage to strip all escape codes (e.g. \x1b[?25l).

This little sed command should do the trick:

./somescript | sed -r 's/[\x1B\x9B][][()#;?]*(([a-zA-Z0-9;]*\x07)|([0-9;]*[0-9A-PRZcf-ntqry=><~]))//g'

The regex is a modification of https://github.com/acarl005/stripansi/blob/master/stripansi.go#L7

Yair Halberstadt
  • 5,733
  • 28
  • 60
0

I came across this question/answers trying to do something similar as the OP. I found some other useful resources and came up with a log script based on those. Posting here in case it can help others.

Digging into the links helps understand some of the redirection which I won't try and explain because I'm just starting to understand it myself.

Usage will render the colorized output to the console, while stripping the color codes out of the text going to the log file. It will also include stderr in the logfile for any commands that don't work.

Edit: adding more usage at bottom to show how to log in different ways

#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

. $DIR/dev.conf
. $DIR/colors.cfg

filename=$(basename ${BASH_SOURCE[0]})
# remove extension
# filename=`echo $filename | grep -oP '.*?(?=\.)'`
filename=`echo $filename | awk -F\. '{print $1}'`
log=$DIR/logs/$filename-$target

if [ -f $log ]; then
  cp $log "$log.bak"
fi

exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$log 2>&1


# log message
log(){
    local m="$@"
    echo -e "*** ${m} ***" >&3
    echo "=================================================================================" >&3
  local r="$@"
    echo "================================================================================="
    echo -e "*** $r ***" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g"
    echo "================================================================================="
}

echo "=================================================================================" >&3
log "${Cyan}The ${Yellow}${COMPOSE_PROJECT_NAME} ${filename} ${Cyan}script has been executed${NC}"
log $(ls) #log $(<command>)

log "${Green}Apply tag to image $source with version $version${NC}"
# log $(exec docker tag $source $target 3>&2) #prints error only to console
# log $(docker tag $source $target 2>&1) #prints error to both but doesn't exit on fail
log $(docker tag $source $target 2>&1) && exit $? #prints error to both AND exits on fail
# docker tag $source $target 2>&1 | tee $log # prints gibberish to log
echo $? # prints 0 because log function was successful
log "${Purple}Push $target to acr${NC}"


Here are the other links that helped:

Carlos Soriano
  • 669
  • 6
  • 6
0

I used perl as I have to do this frequently on many files. This will go through all files with filename*.txt and will remove any formatting. This works for my use case and may be useful for someone else too so just thought of posting here. replace whatever your file name is in place of filename*.txt or you can put file names separated by spaces in setting the FILENAME variable below.

$ FILENAME=$(ls filename*.txt) ; for file in $(echo $FILENAME); do echo $file; cat $file | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > $file-new; mv $file-new $file; done
Jiangge Zhang
  • 4,298
  • 4
  • 25
  • 33
jimmax777
  • 11
  • 1
  • 2
0

my contribution:

./somescript | sed -r "s/\\x1B[\\x5d\[]([0-9]{1,3}(;[0-9]{1,3})?(;[0-9]{1,3})?)?[mGK]?//g"
stu
  • 8,461
  • 18
  • 74
  • 112
-2

for macOS

$ my_program | pbcopy && pbpaste
Nakilon
  • 34,866
  • 14
  • 107
  • 142
-8

This works for me:

./somescript | cat
spiderlama
  • 1,539
  • 15
  • 10
  • 5
    That depends on how `somescript` is implemented. It may or may not recognise that its standard output is a tty. (The words offenders actually hard-code terminal-specific escape codes into the program, and break horribly when used on other terminals or in scripts). – Toby Speight Jul 26 '17 at 09:24
  • Thanks Toby. I used django's manage.py to test, but what you said makes sense. – spiderlama Jul 27 '17 at 10:20