17

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?

Simon
  • 10,167
  • 5
  • 57
  • 72
  • I was using Print for outputting warnings in this superuser question. Is there a better way of printing warnings to the terminal? – Simon Jan 25 '12 at 04:17
  • Have a look at Message, which seems to be the usual way to handle warnings in Mathematica (I think). – David Z Jan 25 '12 at 07: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). – Simon Jan 25 '12 at 08: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 '12 at 10:04

2 Answers2

21

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"] ];
Arnoud Buzing
  • 9,801
  • 2
  • 49
  • 58
  • +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) – Simon Jan 25 '12 at 04:44
12

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.

David
  • 14,911
  • 6
  • 51
  • 81