How to: how to create your own command in Linux

Linux command line: wallpaper

We always talk about commands to run in the Linux CLI, consoles, terminal emulators, etc. But this time we bring you a slightly different tutorial, it is a mini-guide to teach create our own Linux command. Yes, as you hear it, in an easy and simple way we can create our own tool and call it from the Linux console to run it and enjoy it. For this we have different options, since we can use different programming languages ​​to create it, although for our example we will simply focus on shell scripting for Bash.

The procedure to create a program or command requires the following steps:

  1. Write the code of our tool. If you already know what you need or what you want, write the source code of your tool whatever it is and whatever language you have chosen. For example, you can do it in C, Python, Perl, or as a script for Bash.
  2. Compile our source code to generate the executable. For example, if it is in C or C ++, etc., you can do it with the help of the gcc compiler in an easy way. If it is an interpreted language, such as Python, Perl, Ruby, etc., we will have to have its interpreter installed and make the file with the source code executable. This is also the case of a script for Bash, in this case the interpreter is Bash itself and to make it executable we can use: chmod + x script_name.sh
  3. Once compiled or we have the executable file, we copy it or move it to a route included in the $ PATH environment variable, such as / usr / bin. You can see the paths with echo $ PATH. With this we can execute it simply by entering its name and we will not have to put the absolute path.

Once this is done we have our command ready to execute ... you can write its name and it will be executed.

For example, for you to understand, I will put a practical example:

  • Step 1: we are going to write the code, in this case a simple bash script, for this open a favorite text editor and write the following code (or that of your script):
#!/bin/bash

echo "Hola mundo"

  • Step 2: we save the text file and in my case I will call it hello. And now I make it executable ;:
chmod +x hola

  • Step 3: now it's time to move it to a known path so as not to always have to be in the directory where it is hosted or put the absolute path for its execution ...
cp hola.sh /usr/bin/

And now we could run it with a simple:

hola

And in this case you should see on the screen a simple message «Hello World«


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

A comment, leave yours

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Emiliano said

    The file should be called hello without .sh if you want to invoke with a simple hello
    Regards!

bool (true)