Node:Running Image Files, Next:, Previous:Stack and Dictionary Sizes, Up:Image Files

Running Image Files

You can invoke Gforth with an image file image instead of the default with the -i flag (see Invoking Gforth):

gforth -i image

If your operating system supports starting scripts with a line of the form #! ..., you just have to type the image file name to start Gforth with this image file (note that the file extension .fi is just a convention). I.e., to run Gforth with the image file image, you can just type image instead of gforth -i image. This works because every .fi file starts with a line of this format:

#! /usr/local/bin/gforth-0.4.0 -i

The file and pathname for the Gforth engine specified on this line is the specific Gforth executable that it was built against; i.e. the value of the environment variable GFORTH at the time that gforthmi was executed.

You can make use of the same shell capability to make a Forth source file into an executable. For example, if you place this text in a file:

#! /usr/local/bin/gforth

." Hello, world" CR

and then make the file executable (chmod +x in Unix), you can run it directly from the command line. The sequence #! is used in two ways; firstly, it is recognised as a "magic sequence" by the operating system1 secondly it is treated as a comment character by Gforth. Because of the second usage, a space is required between #! and the path to the executable (moreover, some Unixes require the sequence #! /).

The disadvantage of this latter technique, compared with using gforthmi, is that it is slightly slower; the Forth source code is compiled on-the-fly, each time the program is invoked.

#!       --         gforth       ``hash-bang''
An alias for \


  1. The Unix kernel actually recognises two types of files: executable files and files of data, where the data is processed by an interpreter that is specified on the ``interpreter line'' -- the first line of the file, starting with the sequence #!. There may be a small limit (e.g., 32) on the number of characters that may be specified on the interpreter line.