Ever found yourself typing the same long sequence of commands over and over? Do you have a handy bash script but are tired of typing bash ~/scripts/my_script.sh every time? There’s a better way.
By making your script executable and placing it in your system’s PATH, you can run it from any directory with a single, custom command name. It’s how you start building your own powerful command-line interface (CLI) tools.
This guide will walk you through the three essential steps to transform any bash script into a bona fide Linux command.
Step 1: Write Your Bash Script
First, you need a script. Let’s create a simple but practical one called mkproj. This script will create a new directory for a project and populate it with a few standard subdirectories: src, docs, and tests.
Create a new file named mkproj:
nano mkproj
Paste the following code into the file:
#!/usr/bin/env bash
# A simple script to create a new project directory structure.
# Usage: mkproj <project-name>
# Exit immediately if a command exits with a non-zero status.
set -e
# Check if a project name was provided as an argument.
if [ -z "$1" ]; then
echo "Error: Please provide a project name."
echo "Usage: $(basename "$0") <project-name>"
exit 1
fi
PROJECT_NAME="$1"
# Create the main project directory and navigate into it.
mkdir "$PROJECT_NAME"
cd "$PROJECT_NAME"
# Create standard subdirectories.
mkdir src docs tests
echo "âś… Project '$PROJECT_NAME' created successfully!"
echo "You are now in $(pwd)"
Save and exit the editor (in nano, press Ctrl+X, then Y, then Enter).
Let’s break down two key parts of this script:
#!/usr/bin/env bash: This is the shebang. It’s a critical line that tells your operating system to execute this file using thebashinterpreter. Using/usr/bin/env bashis slightly more portable than/bin/bashbecause it finds thebashexecutable in your environment’sPATH.$1: This is a special variable that holds the first argument you pass to the script from the command line. In our case, it will be the project’s name.
Step 2: Make the Script Executable
Right now, your mkproj file is just a text file. If you try to run it (./mkproj), you’ll get a “Permission denied” error. You need to give it execute permissions.
We do this with the chmod (change mode) command.
chmod +x mkproj
Here’s what that command does:
chmod: The command to change file permissions.+x: Adds (+) the executable (x) permission to the file for the current user.
Now, you can run your script from the current directory like this:
./mkproj my-awesome-project
The ./ is important—it tells your shell to look for the command in the current directory. Our goal is to get rid of that.
Step 3: Move the Script to a Directory in Your PATH
So, how does your shell find commands like ls, cd, or git without you specifying their full path? It looks for them in a list of directories defined by an environment variable called PATH.
You can see the directories in your PATH by running:
echo $PATH
You’ll see a colon-separated list of directories, such as /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin. To make our mkproj script globally available, we need to move it into one of these directories.
The best practice for user-created scripts is to use ~/.local/bin. This directory is intended for user-specific executables and keeps your system’s directories (/usr/bin) clean.
1. Create the directory if it doesn’t exist:
mkdir -p ~/.local/bin
The -p flag tells mkdir to create parent directories if needed and not to complain if the directory already exists.
2. Move your script:
mv mkproj ~/.local/bin/
This moves the mkproj file into the ~/.local/bin directory. Because we’re moving it, we don’t need to specify a new name.
3. Ensure ~/.local/bin is in your PATH:
On many modern Linux distributions, ~/.local/bin is automatically added to your PATH when you log in. Open a new terminal window and test it:
mkproj my-second-project
If it works, you’re done! If you get a “command not found” error, you need to manually add ~/.local/bin to your PATH.
To do this, add the following lines to the end of your shell’s configuration file (e.g., ~/.bashrc for Bash or ~/.zshrc for Zsh):
# Add .local/bin to PATH
if [ -d "$HOME/.local/bin" ]; then
export PATH="$HOME/.local/bin:$PATH"
fi
After adding this, either start a new terminal session or apply the changes to your current one with source ~/.bashrc (or source ~/.zshrc).
Conclusion
You’ve successfully created a custom Linux command! Now, creating a new project skeleton is as simple as typing mkproj project-name from anywhere on your system.
To recap, the three key steps are:
- Shebang: Start your script with
#!/usr/bin/env bash. - Permissions: Make it executable with
chmod +x your_script. - Location: Move it to a directory in your
$PATH, like~/.local/bin.
From here, you can create all sorts of custom CLI tools to automate your daily tasks, from simple file operations to complex backup routines. Happy scripting