Create Your Own Bash Command

You may want to have a quick command for personal use, on your Linux machine. Say you want a quick custom converter between currencies, or a quick calculation of time, or anything that you do regularly in your head that you would love to have a terminal command for.

The steps to create a command are simple:

  1. In any folder of your own, create a file with the name of the command.

    cd myfolder
    touch mycommand

  2. Open the file with any text editor and write a simple script that does what you need and outputs your result.

    For example, a simple multiplier for quick conversions:

    #!/bin/bash

    if [ $# -eq 0 ]
    then
    echo “No arguments supplied”
    exit
    fi

    rate=60
    converted=$(($1*rate))
    echo
    echo $1 hours = $converted minutes
    echo

  3. Copy the finished script to your local bin (which is usually part of your PATH already).

    sudo cp mycommand /usr/local/bin/

 

Now you can execute your command just by opening terminal and typing the command name along with the arguments.
eg. mycommand 10

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s