How to run an Octave script from command line

Last updated on June 16, 2020 by Dan Nanni

GNU Octave is an open-source alternative to MATLAB, a proprietary software and programming framework for conducting numerical mathematics and data analysis. If you are an Octave user, you are probably familiar with its interactive shell environment.

However, if you want to perform Octave-based analytics non-interactively in a batch processing environment, you need to be able to run Octave scripts from the command line.

Here is a simple Octave script example which you can use. The script accepts command line arguments.

$ vi hello.m
#!/usr/bin/octave -qf

# example octave script

arg_list = argv ();
num = str2int(arg_list{1});

printf ("Name of Octave script: ", program_name ());

for i=1:num
   a(i) = i;
elapsed = toc();

printf("Elapsed time: %.4f seconds", elapsed);

Don't forget to make the script executable:

$ chmod 755 hello.m

At this point you can simply run the script as an executable.

$ ./hello.m 10000

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean