Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

When I created a Mathematica script, I found that all my lines were commented out (when opened with vi or gedit or any other text editor). So I either need to get rid of the (**) manually or through a bash script before I run my Mathematica script from a unix terminal.

To create my script, I include the shebang #!/usr/local/bin/MathematicaScript -script on the first line and then save my file as a .m package. The resulting file looks something like this:

(* ::Package:: *)

(* ::Input:: *)

(* ::Input:: *)
(*f[x_] := x*)

Why are my lines commented? Is there anything I need to do?

share|improve this question

1 Answer 1

up vote 17 down vote accepted

The reason why all the lines are commented is because you are using the frontend to create your script in input cells and saving it as a .m file (equivalent to choosing "Mathematica Package" in the Save As dialog).

Now to create a Mathematica package file (or .m file), the code needs to be in initialization cells or code cells. Content in any other cell is commented out. So the change you need to make is to select your input cells and choose Cell > Cell Properties > Initialization Cell or enter your code in a code cell (Cmd8 on a mac). If you save this file, you'll find the code present uncommented in the saved file.

share|improve this answer
Oh my! Thats a nuance I was not aware of. Will try it at once. –  drN Aug 27 '12 at 17:57
What do you know! Your suggestion worked! Thanks. –  drN Aug 27 '12 at 18:02
@DNA: It's a blessing in that you can leave all sorts of other stuff -- especially text cells, Section, etc., cells -- for purposes of documentation, test examples, etc., in the source .nb notebook, then just save the notebook as a .m package. –  murray Aug 27 '12 at 22:15
@DNA or you could switch over to working in the .m package directly. –  Yves Klett Aug 28 '12 at 5:03
@DNA Yves meant that you could also use the front end's package editor to create the .m files. You can get to it by going to File > New > Package or if you open a .m file in Mathematica (notice it looks different from your usual front end notebook). This has nothing to do with how the file is run –  rm -rf Aug 28 '12 at 14:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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