Create your own Linux command using Bash

Improve your experience in the terminal by creating your own command-line interface.

Sam
5 min readNov 23, 2019
Photo by Sai Kiran Anagani on Unsplash

I just want to execute my own command in a terminal. — you.

Have you ever just wanted to have a handy command to execute your script in your terminal? In this article, I’ll teach you how you can do just that using an executable bash script.

Note: For Linux Users.

We’ll cover:

  • How to create and run a basic bash Script.
  • How to make it executable.
  • Learn about the PATH environmental variable.
  • Add it to the PATH to execute our script as a command on the terminal.
  • Doing something more interesting in our script using curl and jq to create a currency converter CLI.

Basic Bash Script.

Bash script Language helps us to write custom functionality and to automatize all kinds of process. In short, all the commands that you can run in a terminal you can put them in a Bash Script. If you aspire to be a DevOps Engineer or a programmer that is constantly executing repetitive commands or you just want to learn more about Bash, this is for you.

First lets create a file called converter and put the following code:

#!/bin/bash
echo "Hello, world"

You can now execute it with like this:

$ bash converter
Hello, world

As you could see the String Hello, World was displayed. Now, first thing to notice is this strange comment in the first line #!/bin/bash This is called a shebang is the character sequence #! and it tells the shell which program to interpreter the script with, in this case with our bash shell.

Make it executable

This means to execute it as ./converter instead as bash converter and to do this we’ll use chmod to change the the mode to executable.

$ chmod +x converter

With this we can now run it like this

$ ./converter
Hello, world

The PATH environmental variable

Let’s talk a little about the PATH so you know what we are doing here. Please bare with me on this.

The idea is simple: PATH is a variable that helps your terminal find which program to execute when you type something in it.

PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executable files (i.e., ready-to-run programs) in response to commands issued by a user — LINFO

So let us see that variable

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

A user’s PATH consists of a series of colon-separated absolute paths that are stored in plain text files. Whenever a user types a command at the command line that is not built into the shell or that does not include its absolute path and then presses the Enter key, the shell searches through those directories, which constitute the user’s search path, until it finds an executable file with that name. — LINFO

So if I type something like godzilla in the terminal our shell first will search for that executable file in /usr/local/sbin/godzilla if it can’t find it there, it will search on the next path and so on, if it’s not on the path it will simply log something like:

godzilla: command not found

Add it to the path

Alright, all very interesting, how can I change this variable? you can change it in the current session (i.e only in the terminal you’re currently setting it) or permanently through all sessions.

If you want to change it temporarily just execute the following

$ export PATH=$PATH:/home/my-user-name/.local/bin

What we really want is to be set through all sessions when we log in so the path to our executable is available when we open a terminal. This depends on the type of shell we’re running because each one has their own profile configuration file.

Here’s a list of some types of shells with their configuration files:

  • Bourne shell (sh) — .profile
  • C shell (csh) — .login
  • TC shell (tcsh) — .login
  • Korn shell (ksh) — .profile
  • Bourne Again shell (bash) — .bash_profile or .bashrc

You can check which one you’re using with

$ echo $0

Please note that if you log in graphically, ~/.profile will be specifically sourced by the script that launches your desktop environment, so the most advisable place to add the path is in this file. For Ubuntu and Mint users .bashrc would be a good candidate.

With that in mind just open the configuration file ~/.profile and append the following (if it’s not already there)

if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi

That lines just says: “If the following directory ~/home/my-user-name/.local/bin exist, add it to the path ”.

We’re using ~/.local/binbut it can actually be anywhere, convention says to place it on ~/local/bin/, but there’s a great discussion surrounding that, besides that you will need to move files using your admin privileges. So we’ll better use this one ~/.local/bin/. Now, let us create that directory.

$ mkdir -p ~/.local/bin

The -p flag or --parents create the missing directories in the path.

Let us move our script there.

$ mv converter ~/.local/bin/converter

The change won’t happen until you log out and log in again.

That’s about it! now you can call you script from everywhere, try it! change directory and call it like this:

$ converter
Hello, World

you can be sure that it’s your script that’s running by using which

$ which converter

Please do check if there’s any program by the name of the file that you’re creating.

If you choose to use .bash_profile instead of .profile this is the way you add it to the PATH .

$ echo 'export PATH=~/.local/bin:$PATH' >> ~/.bash_profile
$ source ~/.bash_profile

By running source you are loading what’s in .bash_profile to the session

Improving our script

Now that we set out script we now can make it do something more interesting than just printing a greeting. We’ll create a CLI currency converter from USD to EUR.

We’ll call a currency converter API and make our script use it. For that, we’ll make use of a pretty fun command-line JSON processor called jq to parse the response and curl to make the request.

You can install jq like this: sudo apt-get install jq

Let us open our converter and place the following code.

#!/bin/bash
# API source: https://www.exchangerate-api.com/
USD=$(curl -s https://api.exchangerate-api.com/v4/latest/USD | jq .rates.EUR)
CONVERSION=$(echo "$USD * $@" | bc)
echo "$@ USD -> $CONVERSION EUR"

Let’s use it!

$ converter 1
1 USD -> 0.91 EUR

I hope you enjoyed this article and that it helped you to grow a little more in your craft.

About me

I’m a Software Engineer, writer, tech enthusiast, pianist, origami lover, amateur photographer. In my spare time I go trekking, play the piano and learn history.

My tech: JavaScript, Node.js, React, Ruby, Crystal, Bash, Docker.

You can follow me on Twitter, LinkedIn or visit my page to contact me.

--

--