This page looks best with JavaScript enabled

How to Make Your Shell Script Into a Command in Linux

 ·  ☕ 3 min read  ·  🤖 Kaung

Context

In Linux environments, shell scripts are essential for making life easy. At times, while working in console, we know that it can get tedious and painful to just type ./script.sh or path/to/script.sh to invoke it over and over again. In this article, I will share how to make a shell script into a command in linux, which we can then invoke from anywhere with less typing.

Of course, the benefit is not just about less typing. This will also allow us to build more accessible and elegant custom system tools.

How To

Depending on the needs and situation, there are a few ways to do this. For this set up, let us assume that we just want the command for peronal use.

Here is a sample script. Let’s call it saferm. It safely moves a file to a recycle bin of choice. For simplicity sake, we are only calling saferm on one file at time.

1
2
3
4
5
#!/bin/bash

RECYCLE_BIN="/tmp"

mv -v $1 "${RECYCLE_BIN}/"

Save the script and make it executable.

1
chmod u+x ./saferm

To make it a command, we include it in a bin directory. For personal use, we can include it in $HOME/bin (self-explanatory). We also check if that directory exits and is included in $PATH.

Side Note on $PATH

$PATH is a core linux environment variable which lists executable commands and makes them available upon system start-up. Once we invoke a command (for example “ls” to list files), shell figures out the location of the command via $PATH variable and executes it.

We can check where “ls” is located (or stored) in the system. See the commands below.

1
2
3
echo $HOME
echo $PATH
which ls

Coming back to our saferm - we are basically making it available in one of the directories listed in $PATH. In case of Ubuntu, we store personal commands in $HOME/.local/bin - so now we just move it there.

1
2
3
# Create the dir if it doesn't exit
# mkdir $HOME/.local/bin
mv ./saferm "$HOME/.local/bin/"

Now that we have moved it to the directory. Let’s make sure if the directory path is included in $PATH.

If we don’t find it there, we will have to re-export $PATH. This can be done in the start-up environment script. It really depends on the system, but for Ubuntu, we can find .profile file under $HOME.

Then we just add below line in at the end of the file.

1
export PATH=$HOME/.local/bin:$PATH

With that, we can now invoke the command from any directory in command line. Below is the actual walk through in console.

Demo Screenshot

Summary

In this article, I have shared how to make a shell script into a personal command in Linux (Ubuntu). I have also shared the purpose of $PATH environment variable, how it makes Linux commands avaiable in shell environment, and the profile script which exports such variables at system start-up.

If you have questions or comments, you can reach me at hello@kaung.dev.

Note: the demo script saferm - its full version is available on my github.

Cheers!

Share on
Support Me

kaung
WRITTEN BY
Kaung
Software Engineer | Physicist