0

For some commands, in the terminal they print with some colors in the stdout, for example:

git status
mvn help:help -Ddetail=true
gradle build
Any Linux command (ls [-...], etc)

Note: it applies for scripts that contain:

  • Executions of Linux commands
  • Executions for tools commands
  • Executions other scripts

Therefore the following is possible:

./mvnw help:help -Ddetail=true
./gradlew build
./customscript.sh

Until nothing is new and all work how is expected, therefore:

  • linux_command
  • tool_command (maven, git, gradle etc)
  • script.sh (execute linux/tools commands and other scripts)

So if any of them print in the terminal (stdout) some colors, it is the default behavior according each command/tool

Now if I want see the output in the terminal (as above) and write it to some file, according with:

Therefore is possible do in general

"linux_command" | tee [-a] "/some/path/log_file.log"
"tool_command"  | tee [-a] "/some/path/log_file.log"
"script.sh"     | tee [-a] "/some/path/log_file.log"

And it works how is expected, but the output in the terminal (stdout) does not include the colors anymore.

Question:

  • How to show the output for the execution for any command and/or script in stdout and file but keeping the color in the stdout?

Same behaviour when the pipe and tee were not included and of course meanwhile write the content in the .log file.

Note I did do a research about the script command

but it overrides the script.sh content

I need a general approach, it for any command and/or script.sh

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158

1 Answers1

1

unbuffer is your command

unbuffer git status | tee => I keep my color

git status | tee => back in no-color world

You can have :

  • on Debian/Ubuntu with expect package
  • on Fedora with expect package
  • On MacOS with brew.sh expect package
  • On MacOS with macports.org expect package

Source of expect package is https://core.tcl-lang.org/expect/index

FYI , if your problem is only limited to Git , Git has a flag to force color .

git -c status.color=always status
EchoMike444
  • 1,513
  • 1
  • 9
  • 8
  • Does `unbuffer` work with any command and script, right? - sadly my mac is very old, I can't upgrade homebrew. Is other way to install in MacOS? El Capitan. For Linux I am OK in other machines – Manuel Jordan Aug 08 '21 at 14:13
  • pls if you know other approach without install any dependency would be perfect - it mostly for Windows where I execute the scripts after of the execution of the `bash` command available from `Git` (declared in the PATH) – Manuel Jordan Aug 08 '21 at 14:32
  • @ManuelJordan you are using `bash` in which context ? WSL 1 ? WSL 2 ? Cygwin ? – EchoMike444 Aug 08 '21 at 23:52
  • about `Windows 10` I use `ConEmu` (for multiple tab pages) and `Git` is added in the OS' `PATH`, so when I open `ConEmu` and even `cmd` and I write the `bash` command it loads the `Git` environment and is possible use a good set of default of Linux commands - but of course is not possible 'install' some packages - or is possible? – Manuel Jordan Aug 09 '21 at 00:27