Can I Make My Own Linux Commands?

Karthikaa R
5 min readJul 2, 2024

--

Yes, it is possible! By the end of this blog, you’ll be able to create your own commands on Linux. Here’s how:

Method 1: Using the $PATH Variable (file without extension )

The $PATH environment variable is a critical component of Linux that determines where the system looks for executable files. By placing your custom commands in a directory that’s included in $PATH, you can run these commands from anywhere.

For Method 1 in this tutorial, I’ll demonstrate creating a command named ‘say’. The function of ‘say’ is simple: when executed with a name

say chitra

it outputs ‘Hi chitra’ . This example will help you understand how to create custom commands for various purposes and experiment with them.

Steps:

  1. Check for $PATH and Change Directory:

First, check your current $PATH settings to find a suitable directory. You can echo $PATH to see the directories listed. Navigate to one of these directories using the cd command.

I’m using ‘/usr/bin’ as the default. Feel free to select any other directory from the $PATH that you’d like to use instead.

2. Create a Python File without Extension:

Using nano or any text editor with administrative privileges (sudo), create a Python file without an extension. This example will create a simple command that echoes a message.

Enter the following Python code:

#!/usr/bin/env python3

import sys

# Check if an argument is provided
if len(sys.argv) != 2:
print("Usage: say <name>")
sys.exit(1)

# Get the name from the command line argument
name = sys.argv[1]

# Print the name and its length
print("Hi, ",name)

Save the file (Ctrl+X, Y, Enter) in linux.

NOTE: [Replace this code with your own: add your custom functionality to make this script do something useful]

Step 4: Make the File Executable:

To make the file executable, use chmod with '+x’ making the file executable

Step 5: Test Your Command

Now, you can use your command from anywhere in the terminal

All commands used in this method:

$PATH
cd /usr/bin
sudo nano say
# insert the code you want
sudo chmod +x say
say nimi
# output: Hi nimi

Ultimately, you might wonder how the Python file was created without an extension. This is a common feature in operating systems that often goes unnoticed. In Linux, file types are recognized by content and permissions rather than relying solely on file extensions. In contrast, Windows users often identify file types based on icons, sometimes overlooking the absence of file extensions, especially with common types like documents or scripts.

Method 2: Using the $PATH Variable (file with extension )

The $PATH environment variable is a critical component of Linux that determines where the system looks for executable files. By placing your custom commands in a directory that’s included in $PATH, you can run these commands from anywhere.

For Method 2 in this tutorial, I’ll demonstrate creating a command named ‘said’. The function of ‘said is simple: when executed with a name

said chitra

it outputs ‘This ia said command chitra’ .

Steps:

  1. Check for $PATH and Change Directory:

First, check your current $PATH settings to find a suitable directory. You can echo $PATH to see the directories listed. Navigate to one of these directories using the cd command.

I’m using ‘/usr/bin’ as the default. Feel free to select any other directory from the $PATH that you’d like to use instead.

2. Create a Python File without Extension:

Using nano or any text editor with administrative privileges (sudo), create a Python file without an extension. This example will create a simple command that echoes a message.

Enter the following Python code:

#!/usr/bin/env python3

import sys

# Check if an argument is provided
if len(sys.argv) != 2:
print("Usage: say <name>")
sys.exit(1)

# Get the name from the command line argument
name = sys.argv[1]

# Print the name and its length
print("This ia said command ",name)

Save the file (Ctrl+X, Y, Enter) in linux.

NOTE: [Replace this code with your own: add your custom functionality to make this script do something useful]

Step 4: Make the File Executable:

To make the file executable, use chmod with '+x’ making the file executable

Step 5: Execute the file

Now, we can’t execute the file directly by its name. This Python file requires the ‘./’ prefix along with its extension to run

Step 6: Link the file

To avoid using the extension every time, we link the ‘said.py’ file to a folder named ‘said’, acting like a shortcut.

As a result, we can now use the command directly.

Step 7: Test your command

All commands used in this method:

$PATH
cd /usr/bin
sudo nano said.py
# insert the code you want
sudo chmod +x say
./said.py nimi
# output: This ia said command nimi
sudo ln -s /usr/bin/said.py /usr/bin/said
said nimi
# output: This ia said command nimi

Now you’re empowered to create your own commands in Linux! Stay tuned for Part 2 where we dive deeper into custom command creation in your own directories. Enjoy exploring Linux!

--

--

Karthikaa R

Cybersecurity student simplifying the cyber realm for all.