16
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ I was using Print for outputting warnings in this superuser question. Is there a better way of printing warnings to the terminal? $\endgroup$ – Simon Jan 25 '12 at 4:17
  • $\begingroup$ Have a look at Message, which seems to be the usual way to handle warnings in Mathematica (I think). $\endgroup$ – David Z Jan 25 '12 at 7:50
  • $\begingroup$ @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). $\endgroup$ – Simon Jan 25 '12 at 8:05
  • $\begingroup$ not that this matters, but ./hello should also work on OS X if the script is executable (and you have the right shebang) $\endgroup$ – acl Jan 25 '12 at 10:04
20
$\begingroup$

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"] ];
$\endgroup$
  • $\begingroup$ +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) $\endgroup$ – Simon Jan 25 '12 at 4:44
12
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ +1 Thanks David. I'd never used WriteString before. $\endgroup$ – Simon Jan 25 '12 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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