Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to Run a CLI With Machine Code
btfuss (177)

How to Make an CLI with Machine Code

This tutorial assumes you have basic knowledge of bash commands and of coding

Step 0: Cheat Replit
Since replit.com doesn't provide a machine code repl, we need to make it work ourselves. Now, this is technically cheating, however, there is no other way to run the machine code so just take it that way. You have 2 options:
1. Make a bash repl and do the work in main.sh
2. Make a bash repl and add the main.sh and the .replit file

Now, either way, you are going to be creating the main.sh file, so I would just choose the first option. Next, make a boot.cpp file. The last file you need to make is os.sh.
Your current directory should look like this:
| - main.sh
| - boot.cpp
| - os.sh

Now, in the boot.cpp file, we need to get bash running with machine code, and cpp is the perfect candidate because it compiles to machine code! So, the 5 lines of code that get bash running are:

#include <stdlib.h>
int main() 
	system("bash os.sh");

To compile this code into machine code, run (in the shell) gcc boot.c -o boot which now gives us another file named boot with no file extension. Finally, delete the boot.cpp file. Your directory should now look like this:
| - main.sh
| - boot
| - os.sh

Keep in mind, the main.sh is only to start the machine code.

Step 1: Get Bash Running
In the main.sh file, type ./boot which will start the machine code which will start the system! To make sure this is working, in the os.sh file type:

echo "Bash is working!"

If you are having any issues, make sure that you had no typos in the boot.cpp from earlier.

Step 2: Run Commands
Create a folder named commands. Then inside, make a file (do not add a file extension) named test. For these commands, I would recommend using interpeted languages, however here is how to run both.

Python: To run a python command, in the a file (lets use test) type at the top: #!/usr/bin/envpython which tells bash to run the python script.

Compiled: Now, I haven't found a better way to do this. First, make a compiled folder, then compile your program and the file without an extension should be put in the compiled folder with its respective program name. Make a file with the same name in the commands folder (1 directory backwards), make sure to not use any file extension, and input the following code:


If that threw an error, replace the second line with:

Make sure to replace the [name] with the actual name without brackets.

Step 2.5: Check Directories
1. No compiled files:
| - main.sh
| - boot
| - os.sh
| - commands
| - - test
Add any other commands you want.

  1. Includes some compiled files:
    | - main.sh
    | - boot
    | - os.sh
    | - commands
    | - - test
    | - - compiled
    | - - - test
    The test with the three hyphons is compiled and the one with the 2 hyphons has the bash-type script.

Step 3: Get the CLI Functioning
Now remove the third line of code (unless you want to know bash is working) from the os.sh file and add in the following code:

export PATH="$(pwd)/commands:$PATH" && chmod  -R +x ./

Lets break this down line by line:

  1. export PATH - This is the way bash is getting its commands from our folder
  2. $(pwd)/commands:$PATH - Defines folder
  3. bash - Starts the CLI (Command Line Interface)!

When you start the program, the line should say: [email protected][number] in green, which is normal. That's where you type in your commands. Welp, thats all I have today, peace!

I spent actual time on this upvote

In the project below, I connected bash to some python scripts in a different folder. Just type in the name of one of the files (in the commands folder) and a hopefully non-buggy script will run!

DynamicSquid (4899)

Ooh, interesting! I really like these low level tutorials :)