Hello World (first programs) · Beginner · 14 min
Hello World in GNU Octave: scripts, matrices and MATLAB-style code
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 (gnuoctave/octave:11.3.0 image) · GNU Octave 11.3.0 · Ubuntu 26.04 apt ships Octave 11.1.0
You will install GNU Octave 11.3, print Hello World from the command line and from a script, build matrices, see why A * B and A .* B give different answers, and write your first function file.
On this page
GNU Octave is a free language for numerical computing, and it is deliberately close to MATLAB: most MATLAB scripts and function files written for a course or an engineering team run in Octave unchanged. That makes it a practical way to work through MATLAB-based coursework, check a lecturer's examples or prototype a calculation without paying for a licence.
The thing that sets Octave apart from general-purpose languages is its basic type. In Python or JavaScript a number is a number; in Octave everything is a matrix. A single number such as 5 is a 1-by-1 matrix, a row of numbers is a 1-by-n matrix, and text is a row of characters. The arithmetic operators work on whole matrices at once, which is why Octave code for maths and engineering tends to be short.
In this tutorial you install Octave 11.3, print Hello World from the command line, then save and run a script. One step further, you build matrices, see the difference between matrix multiplication and element-by-element multiplication, and write a function in its own file. Along the way you meet two habits Octave shares with MATLAB, R and Julia: indexing starts at 1, and a missing semicolon prints the result of a line.
Step 1 · Install GNU Octave
Pick the route for your system. Octave and the libraries it uses are a large download, several hundred megabytes, so on prepaid mobile data it is worth installing over Wi-Fi.
- Ubuntu (apt): Ubuntu 26.04 includes Octave 11.1.0 in its universe repository. Everything in this tutorial works on 11.1. Install it with:
sudo apt update
sudo apt install -y octave- Any Linux distribution (Flatpak): Flathub has the current release, 11.3.0:
flatpak install flathub org.octave.OctaveWith Flatpak, start Octave as flatpak run org.octave.Octave wherever this tutorial says octave.
- macOS (Homebrew):
brew install octaveinstalls 11.3.0. - Windows: download
octave-11.3.0-w64-installer.exefrom the download page at octave.org and run it. It adds Octave to the Start menu. You can type the commands in this tutorial into the Command Window of the Octave window, as shown in each step.
Now check the version in a terminal:
octave --version | head -n 1Check it worked
The terminal prints GNU Octave, version 11.3.0 (or 11.1.0 if you installed from Ubuntu's apt). command not found means Octave is not on your PATH, the list of folders your terminal searches for programs: open a new terminal and try again.
Step 2 · Say Hello, World from the command line
Octave can run a single line of code and exit, without opening a window:
octave --no-gui --eval 'disp("Hello, World!")'What each part does:
octavestarts Octave. On a desktop, running it with no options opens the graphical interface, with a file browser, editor and Command Window.--no-guikeeps Octave in the terminal instead of opening that window. A separate program,octave-cli, does the same thing and never loads the graphical parts at all.--eval '...'runs the code inside the quotes and then exits. The single quotes stop your shell from treating the!or the double quotes specially.disp(...)displays a value followed by a new line. Here the value is the text"Hello, World!".
Check it worked
The terminal prints Hello, World! on its own line and you are back at your shell prompt. On a server or a Linux machine without a desktop, Octave first prints octave: X11 DISPLAY environment variable not set and octave: disabling GUI features. That is a notice, not an error: there is no screen to draw on, so Octave carries on in the terminal. On Windows, type disp("Hello, World!") in the Command Window and press Enter; the same line appears.
Step 3 · Save hello.m and run it
Octave code lives in files ending in .m, the same extension MATLAB uses. Make a folder for this tutorial and create hello.m in it. On macOS or Linux you can paste the whole block into the terminal; on Windows, create the file in the Octave editor with the lines between the OCTAVE markers.
mkdir -p ~/hello-octave-demo
cd ~/hello-octave-demo
cat > hello.m <<'OCTAVE'
% hello.m: my first Octave script
disp("Hello, World!")
name = 'Amara';
count = length(name);
fprintf('Hello, %s. Your name has %d letters.\n', name, count);
fprintf('Running on Octave %s\n', version());
OCTAVE
octave --no-gui hello.mLine by line:
% hello.m: my first Octave scriptis a comment. Octave ignores everything from%to the end of the line. It also accepts#, but MATLAB does not, so%keeps the file portable.disp("Hello, World!")prints the text and a new line, as before.name = 'Amara';stores some text in a variable calledname. Single quotes make a character array: a 1-by-5 matrix of characters. The semicolon at the end stops Octave from printingname = Amaraafter the line runs.count = length(name);counts the elements ofname, which is 5.fprintf('...', name, count);prints formatted text.%sis replaced by a piece of text,%dby a whole number, and\nstarts a new line. Unlikedisp,fprintfadds no new line of its own.version()returns the Octave version as text.octave --no-gui hello.mruns the file from top to bottom and exits.
On Windows, or inside an Octave session on any system, change to the folder (cd works in the Command Window too) and type the file name without .m: hello. Octave finds hello.m in the current folder and runs it.
Check it worked
The terminal prints three lines: Hello, World!, Hello, Amara. Your name has 5 letters. and Running on Octave 11.3.0 (or 11.1.0 on Ubuntu's package).
Single quotes, double quotes and MATLAB
Both quote styles make a character array in Octave, but double quotes also turn \n and similar escapes into the real characters. In MATLAB, double quotes make a different type, a string object. If you want code that runs the same in both, use single quotes for text and fprintf for formatted output, as hello.m does.
Step 4 · Build matrices and index them
Create a second script that builds two small matrices. This time most lines have no semicolon, so Octave prints each result:
cat > matrices.m <<'OCTAVE'
A = [1 2; 3 4]
B = [10 20; 30 40];
corner = A(2, 2)
first_row = A(1, :)
A_transposed = A'
OCTAVE
octave --no-gui matrices.mWhat you wrote:
[1 2; 3 4]builds a matrix. Spaces (or commas) separate the columns and a semicolon starts a new row, soAhas 2 rows and 2 columns.Bis built the same way, but its line ends in a semicolon, so it is not printed.A(2, 2)reads the element in row 2, column 2. Indexing starts at 1, soA(1, 1)is the top-left element, and there is noA(0, 0).A(1, :)reads all of row 1. The colon on its own means "every column".A'is the transpose: rows become columns.
Part of the output looks like this:
A =
1 2
3 4
corner = 4Check it worked
You see A printed as two rows, 1 2 and 3 4, then corner = 4, then first_row as 1 2, and A_transposed with rows 1 3 and 2 4. B does not appear, because its line ended in a semicolon.
Step 5 · Compare A * B with A .* B
This is the line that catches most newcomers. Octave has two kinds of multiplication. Add these lines to the end of matrices.m and run it again:
cat >> matrices.m <<'OCTAVE'
product = A * B
elementwise = A .* B
OCTAVE
octave --no-gui matrices.mThe new part of the output:
product =
70 100
150 220
elementwise =
10 40
90 160
A * Bis matrix multiplication from linear algebra. Each result is a row ofAmultiplied against a column ofBand summed: the top-left value is1*10 + 2*30 = 70.A .* Bmultiplies matching elements:1*10,2*20,3*30and4*40.
The dot version exists for every operator where the two meanings differ: .* for multiplying, ./ for dividing and .^ for powers. When you mean "do this to each number", use the dot. Adding and subtracting work element by element already, so + and - have no dot form.
Check it worked
product shows 70 100 and 150 220. elementwise shows 10 40 and 90 160. If your two results are the same, check that you typed A * B without a dot on the first line.
Step 6 · Write a function file
A function in Octave usually lives in its own file, and the file name must match the function name. Create greet.m:
cat > greet.m <<'OCTAVE'
function message = greet(name)
% greet Return a short greeting for NAME.
message = ['Hello, ' name '. Welcome to Octave.'];
end
OCTAVE
octave --no-gui --eval 'disp(greet("Chidi"))'
octave --no-gui --eval 'help greet'Line by line:
function message = greet(name)starts a function calledgreetthat takes one input,name, and returns one output,message. Whatever is inmessagewhen the function ends is what the caller gets back.- The comment just below the first line is the function's help text.
help greetprints it. ['Hello, ' name '. Welcome to Octave.']joins three character arrays side by side. Because text is a row of characters, joining text is the same as putting matrices next to each other in square brackets.endcloses the function. Octave also acceptsendfunction, but MATLAB only acceptsend.
When you call greet, Octave searches the current folder (and its load path) for a file called greet.m. That is why the file name matters: a function called greet saved as hello_function.m is found as hello_function, not greet.
Check it worked
The first command prints Hello, Chidi. Welcome to Octave. The second prints something like 'greet' is a function from the file .../hello-octave-demo/greet.m, followed by your help line, Return a short greeting for NAME.
Step 7 · Recognise the mistakes beginners make
Each of these produces a message worth knowing on sight.
Using * when you meant .* on two rows of the same length:
octave --no-gui --eval 'x = [1 2 3]; y = [4 5 6]; x * y' || trueOctave stops with an error that says operator *: nonconformant arguments (op1 is 1x3, op2 is 1x3). Matrix multiplication needs the columns of the first to match the rows of the second. x .* y gives 4 10 18.
Counting from 0:
octave --no-gui --eval 'A = [1 2; 3 4]; A(0)' || trueOctave stops with error: A(0): subscripts must be either integers 1 to (2^63)-1 or logicals. There is no element 0; the first element is A(1).
Other common slips:
- A forgotten semicolon inside a loop prints every intermediate value and can fill the screen. End lines with
;unless you want to see the result. - A file name that is not a valid name.
my-script.mruns withoctave my-script.m, but typingmy-scriptinside Octave is read asmyminusscript. Use letters, digits and underscores, starting with a letter:my_script.m. - Spaces inside brackets.
[1 -2]is two numbers, 1 and -2, but[1 - 2]is one number, -1. - Text length is counted in bytes. Character arrays hold UTF-8 bytes, so
length('Chloé')is 6, not 5, becauseétakes two bytes. Keep this in mind when you work with names in Yoruba, French or other languages with accented letters.
When you are finished, remove the demo folder:
cd ~
rm -rf ~/hello-octave-demoCheck it worked
You have seen the nonconformant arguments error and the A(0): subscripts must be error in your own terminal, and ls ~/hello-octave-demo now reports that the folder does not exist.
Where Octave is used
Octave is common in university engineering and science courses, especially signal processing, control systems and numerical methods, where teaching material is written for MATLAB. Researchers and engineers use it to prototype numerical algorithms before moving them into production code, and to run existing MATLAB code on machines without a licence. Extra toolboxes, such as signal and control, come from Octave Forge and install inside Octave with pkg install -forge <name>. Not everything matches MATLAB: some toolboxes have no Octave equivalent, and Simulink is not available. The Octave wiki keeps a list of differences.
Next steps
- Try the same first steps in another language built for data and numbers: Hello World in R.
- See a newer take on fast numerical code, also counting from 1: Hello World in Julia.
- Compare with the general-purpose language most data work now uses: Hello World in Python.
- Read the Octave manual at docs.octave.org, starting with the chapters on matrices and functions.
Related tutorials
- Hello World in R: Rscript, vectors and your first functionYou will install R 4.6, run Hello World in the console and as a script with Rscript, see why every result starts with [1], and write a greet function that takes names from the command line.
- Hello World in Julia: juliaup, the REPL and multiple dispatchYou will install Julia 1.13 with juliaup, use the REPL's help and package modes, run a script that greets you by name, see the first-call compile delay, write a function with two methods and give a project its own package environment.
- Hello World in Python: the interpreter, input and your first scriptYou will print Hello World in the Python 3.15 interpreter, save and run it as a script, greet a person by name from the keyboard and from the command line, and know what Python does with your file when it runs.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials