Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Many languages define a special identifier whose value is the path to the source file currently being processed. (For example, in C, this is done through the __FILE__ preprocessor macro.)

Does Mathematica have anything like this (at least for *.m files)?

(I could not find a tag that adequately represents the topic of this question (namely, something like "source file processing"); suggestions welcome.)

share|improve this question
What about \$InputFileName and \$Input? – bobknight Jul 24 '13 at 17:46
@bobknight, $InputFileName fits the bill. I'll gladly accept it if you post it as an Answer. – kjo Jul 24 '13 at 18:19
up vote 6 down vote accepted

Just to get an answer on record.

As bobknight stated in the comments $InputFileName is bound to a string giving the full path name of the file being read when code loading operations initiated by Get, (<<), or Needs are in the process of loading. The file being loaded can make use this information, but the notebook requesting the load cannot (think of it as if $InputFileName were bound to the path-name inside a Block, which may actually be the case).

$Input is a system variable that works similarly to $InputFileName but only gives the file name.


$Inputis a system variable that works similarly to $InputFileName, but is bound to name of the stream from which the loading file is being read. See my additional answer for more detail.

share|improve this answer
For completeness: in version 7, $InputFileName is undocumented and located in System`Private` context. – Oleksandr R. Jul 26 '13 at 18:04

Some additional details on $Input. The Mathematica documentation states that $Input is bound to the name of the stream from which the loading file is being read. But what is that stream name? It appears to be whatever string Get, (<<), or Needs receive as their first argument.

To test this hypothesis, I made a .m file with the following contents and placed in on my system's desktop.

Print[Row[{"$Input: ", $Input}]]
Print[Row[{"$InputFileNamw: ", $InputFileName}]]

I then evaluated the following expressions from an open notebook:

Get[SystemDialogInput["FileOpen", "*.m"]]

{OutputStream[stdout, 1], OutputStream[stderr, 2], InputStream[/Users/oldmg/Desktop/TestPkg.m, 86]}
\$Input: /Users/oldmg/Desktop/TestPkg.m
\$InputFileName: /Users/oldmg/Desktop/TestPkg.m

SetDirectory[FileNameJoin[{HomeDirectory[], "Desktop"}]];

{OutputStream[stdout, 1], OutputStream[stderr, 2], InputStream[TestPkg.m, 118]}
\$Input: TestPkg.m
\$InputFileName: /Users/oldmg/Desktop/TestPkg.m

Note in the first case, $Input and $InputFileName are bound to the same string. This is because SystemDialogInput returns a full path name. In the second case, however, $Input is bound to a string giving only the file name because that is all that was passed to it.

share|improve this answer

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.