9
$\begingroup$

I am running Mathematica 9 on a linux machine and I've encountered the following behaviour that I am trying to understand. When I'm editing multiple source (.m) files using mathematica saving adds a new line which reads

(* ::Package:: *)

at the start of the file. This interferes with the shebang line that designates a source file as an executable and prevents scripts from working.

I then fix this problem by closing all source files in mathematica and manually removing the line in a text editor - so this is more of an annoyance than an actual problem.

Does anyone know how the line addition can be prevented?

Thanks!

$\endgroup$
3
  • $\begingroup$ Somehow I feel @John Fultz should be able to help here... $\endgroup$
    – Yves Klett
    Commented Jul 9, 2013 at 15:48
  • $\begingroup$ When I open a text file with a .m suffix and a shebang line in Mathematica, it evaluates it as the expression -script + #1!/(Applications Contents MacOS MathematicaScript Mathematica.app)! So you'd have to remove the shebang line to be able to edit the file again in Mathematica... $\endgroup$
    – cormullion
    Commented Jul 9, 2013 at 17:05
  • $\begingroup$ Not only does it add that line, it also mangles all non-ASCII characters. If you have e.g. (* :Author: Pál *), it'll change to (* :Author: P[\AAcute]l *), which is very annoying. For these reasons I try to avoid using the front end to edit .m files. $\endgroup$
    – Szabolcs
    Commented Apr 17, 2015 at 2:23

1 Answer 1

7
$\begingroup$

My advice is don't save your script as a .m file. Consider the following quote from the Mathematica documentation on scripts.

The Mathematica script does not need to have the .m extension. An executable script is a full-featured program equivalent to any other program in a Unix operating system, so it can be used in other scripts, in pipes, subject to job control, etc. Each Mathematica script launches its own copy of the MathKernel, which does not share variables or definitions.

Update

To reply to the question raised by bobthechemist in a comment, there appears to be no difference at all between a .m file and a .wl file created with Mathematica's Package style. Here is image of a short .wl file I created and use regularly.

FUAPatterns

$\endgroup$
3
  • $\begingroup$ I now have this same question. Has there been any solutions? $\endgroup$
    – tarhawk
    Commented Apr 17, 2015 at 0:26
  • $\begingroup$ @tarhawk. I have repaired the link that broke in my answer. I recommend that you should follow it and read the full article on Wolfram Language Scripts. $\endgroup$
    – m_goldberg
    Commented Apr 17, 2015 at 2:01
  • 1
    $\begingroup$ Does anyone know if the same behavior is observed with the .wl extension that seems to be the "new kind of extension"? $\endgroup$ Commented Apr 17, 2015 at 2:50

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.