# Printing a string in a Mathematica script

Here's a simple script saved in the file hello

#!/usr/local/bin/MathematicaScript -script
Print["Hello world"]


I can then run this file using math -script hello or, if the file is set to be executable, ./hello.
In Windows or OSX, run MathKernel -script hello

It prints

"Hello world"

to the terminal.

How can I change this so it does not print the quotation marks?

• I was using Print for outputting warnings in this superuser question. Is there a better way of printing warnings to the terminal? Jan 25, 2012 at 4:17
• Have a look at Message, which seems to be the usual way to handle warnings in Mathematica (I think). Jan 25, 2012 at 7:50
• @David: You're right, but then I need to define a new message, which seemed overkill for a short script (Messages[General] does not contain an appropriate message for me to use). Jan 25, 2012 at 8:05
• not that this matters, but ./hello should also work on OS X if the script is executable (and you have the right shebang)
– acl
Jan 25, 2012 at 10:04

Another option is to set FormatType -> OutputForm on the $Output stream: SetOptions[$Output, FormatType -> OutputForm ];
Print["Hello"];


Or call OutputForm on the string itself:

Print[ OutputForm["Hello"] ];

• +1 That's the solution I came up with after noticing the docs say that for Mathematica scripts, all output is in InputForm. (This was after posting the question) Jan 25, 2012 at 4:44

WriteString is the function you're looking for. It takes two parameters, the first one being the stream you want to write to (in your case standard output, $Output), the second argument is what you want to print. #!/usr/local/bin/MathematicaScript -script WriteString[$Output, "Hello World!\n"]

david@thinkpad:~/temp\$ ./asdf
Hello World!


If you need more general information about streams in Mathematica have a look at the corresponding help page.

• +1 Thanks David. I'd never used WriteString before. Jan 25, 2012 at 4:45