How to run an Octave script from command line

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 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);
$ chmod 755 hello.m

Then, you can simply run the script as an executable.

$ ./hello.m 10000

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

Leave a comment

Your email address will not be published. Required fields are marked *