I want to print to the console in color from a wolframscript. I tried just using a Style:

Print[Style["This is a red string", Red]]

This works fine in a notebook but, not surprisingly, doesn't work from a wolframscript.

How can this be accomplished? Also, is there any issues with terminal compatibility?


2 Answers 2


Here is a Wolfram script that uses ANSI escape codes to change the color. This works in a Gnome terminal (Linux).

#!/usr/bin/env wolframscript

(* MMA.SE Question 164468 -- How to print in color *)

esc = Association["reset" -> "\033[1;0m",
   "black" -> "\033[1;30m", "red" -> "\033[1;31m",
   "green" -> "\033[1;32m", "yellow" -> "\033[1;33m",
   "blue" -> "\033[1;34m", "magenta" -> "\033[1;35m"];

Print[esc["red"], "This is a red string", esc["reset"]]
Print["This is the normal terminal color"]

Print["Green turns to ", esc["red"], "red"]
Print["and it stays red until you change the color"]


How it works: we print an escape code to change the color of the text, we print the text, then we change the color back. If we don't change the color back, it stays that way in the terminal, even after the program ends. If this happens, we can use the GNU/Linux reset command, or tput init to restore the terminal to its default settings. Comment out the last line of the script, if you want to see this.

The escape code esc["reset"] resets the color back to the terminal default, which is dark grey on my system. This is different than esc["black"]. A few more colors are possible.

There may be some terminal issues, especially if you are using Windows and DOS. Sometimes a DOS terminal will interpret the escape codes correctly after we install the ANSI.sys device driver. If you need the above script to work under Windows, you should be able to either install the ANSI.sys driver or install a different terminal.


The only way I could think to do it was to pass an echo terminal command with the proper terminal commands embedded. The commands I used may be local to the particular terminal emulation I am running on OS X, so I don't know if they will work for you, but here is my script, which I saved in the file /Users/oldmg/Desktop/red_string

#!/Applications/Mathematica.11.1.1.app/Contents/MacOS/WolframScript -script

Run @ "echo \"$(tput setaf 1)This is a red string$(tput setaf 0)\""

And here is what happened when I ran it in Terminal.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.