Next: , Previous: Errors, Up: Getting Started

2.6 Executable Octave Programs

Once you have learned Octave, you may want to write self-contained Octave scripts, using the `#!' script mechanism. You can do this on GNU systems and on many Unix systems 1

For example, you could create a text file named hello, containing the following lines:

     #! octave-interpreter-name -qf
     # a sample Octave program
     printf ("Hello, world!\n");

(where octave-interpreter-name should be replaced with the full file name for your Octave binary). After making this file executable (with the chmod command), you can simply type:


at the shell, and the system will arrange to run Octave as if you had typed:

     octave hello

The line beginning with `#!' lists the full file name of an interpreter to be run, and an optional initial command line argument to pass to that interpreter. The operating system then runs the interpreter with the given argument and the full argument list of the executed program. The first argument in the list is the full file name of the Octave program. The rest of the argument list will either be options to Octave, or data files, or both. The `-qf' option is usually specified in stand-alone Octave programs to prevent them from printing the normal startup message, and to keep them from behaving differently depending on the contents of a particular user's ~/.octaverc file. See Invoking Octave. Note that some operating systems may place a limit on the number of characters that are recognized after `#!'.

Self-contained Octave scripts are useful when you want to write a program which users can invoke without knowing that the program is written in the Octave language.

If you invoke an executable Octave script with command line arguments, the arguments are available in the built-in variable argv. See Command Line Options. For example, the following program will reproduce the command line that is used to execute it.

     #! /bin/octave -qf
     printf ("%s", program_name);
     for i = 1:nargin
       printf (" %s", argv{i});
     printf ("\n");


[1] The `#!' mechanism works on Unix systems derived from Berkeley Unix, System V Release 4, and some System V Release 3 systems.