BASIC, which stands for “Beginner’s All-purpose Symbolic Instruction Code,” was one of the earliest and most accessible programming languages. Created in the 1960s, it became immensely popular in the 1980s due to its ease of use and was widely used in education and on early personal computers.
This guide will show you how to run BASIC code directly in a Linux terminal using various tools and interpreters.
Setting up a BASIC Interpreter on Linux
To run BASIC programs in a Linux terminal, you need a BASIC interpreter. Fortunately, several interpreters are available, including BASIC-256, FreeBASIC, and cbmbasic. In this guide, we will use cbmbasic, which is a reimplementation of the original Commodore 64 BASIC.
You can install cbmbasic using the default package manager in many Linux distributions. If you are using a Debian-based system like Ubuntu, use the following command:
apt install cbmbasic
For Red Hat-based distributions, you can use:
yum install cbmbasic
Once installed, you will be able to run BASIC code in the terminal.
Writing Your First BASIC Program
Let’s start by writing a simple BASIC program. You can use any text editor, such as nano, vim, or gedit, to create a new BASIC file.
Open your text editor and type the following lines of code:
10 PRINT "Hello, World!"
20 END
Save the file as hello.bas.
Explanation:
- Line 10: The PRINT statement is used to display a message on the screen.
- Line 20: The END statement indicates the end of the program.
Running BASIC Code in the Terminal
Now that you have created your first BASIC program, you can run it using the cbmbasic interpreter.
To execute your BASIC program, type the following command in your terminal:
cbmbasic hello.bas
The cbmbasic interpreter reads the hello.bas file and executes the code, displaying the message “Hello, World!” in the terminal.
Hello, World!
Interactive Mode in BASIC Interpreters
In addition to running scripts, cbmbasic also allows you to start an interactive BASIC session. This can be useful for experimenting with code and learning how BASIC works in real time.
To start an interactive session, type:
cbmbasic
You will see a prompt where you can enter BASIC commands directly.
At the interactive prompt, you can type:
PRINT "This is BASIC in action!"
Output:
This is BASIC in action!
You can also perform calculations or try different commands without writing a full program file.
Practical Use Cases and Examples
BASIC may be simple, but it can still perform interesting tasks. Here are a few practical examples.
Example 1: Writing a Loop to Print Numbers from 1 to 10
Open a text editor and write the following code:
10 FOR I = 1 TO 10
20 PRINT I
30 NEXT I
40 END
Save the file as count.bas and run it:
cbmbasic count.bas
Output:
1
2
3
4
5
6
7
8
9
10
Example 2: Creating a Simple Calculator Using INPUT Statements
Create a new BASIC file with the following code:
10 INPUT "Enter first number: "; A
20 INPUT "Enter second number: "; B
30 PRINT "The sum is "; A + B
40 END
Save the file as calculator.bas and run it:
cbmbasic calculator.bas
Output:
Enter first number: 5
Enter second number: 7
The sum is 12
Conclusion
In this guide, we explored how to run BASIC code in the Linux terminal, from setting up an interpreter like cbmbasic to writing and executing BASIC programs. BASIC is a great way to learn programming concepts, and revisiting it can be a fun and educational journey. You can now easily run BASIC code on dedicated server hosting from Atlantic.Net!