How to create my own terminal commands [duplicate]

Solution 1:

The easiest way to make Bash scripts that are available to all users of your computer is to place them in /usr/local/bin. This requires you to have admin privileges though.

  1. Create you script:

    Open your favourite text editor and write all the commands you wish the script to run, one command per line.

    You can add comments to Bash scripts by beginning a line with #.

    When you're done, add this exact line below as first line at the top of your script. It's called a "shebang" and tells the shell with which interpreter to execute your script.

    #!/bin/bash
    

    Here's a simple example of a full script, running the two commands you mentioned in your question:

    #!/bin/bash
    
    # Compile 'file':
    gcc file -o file
    # Compile 'file1':
    gcc file1 -o file1
    
  2. Move your script to the correct location:

    The location where you should place own scripts for all users is /usr/local/bin.

    As this directory is owned by user root, you must be an admin and use sudo to move files there.

    I assume you created your script in your home directory, ~/my_script.sh. Simply open a terminal and type the command below, replacing SCRIPTNAME with the name you want to give it. This name will be the command you have to type to run it. The .sh file extension is not necessary.

    sudo mv ~/myscript.sh /usr/local/bin/SCRIPTNAME
    
  3. Set the correct owner and permissions:

    The script should be owned by and writable for root, but readable and executable for everyone. The two commands below ensure this:

    sudo chown root: /usr/local/bin/SCRIPTNAME
    sudo chmod 755 /usr/local/bin/SCRIPTNAME
    
  4. Run your script and enjoy:

    Now you can simply type SCRIPTNAME (or however you called it) to your terminal and the script will get executed.

Solution 2:

While writing scripts is a very convenient way to execute many commands from one file , I would suggest using functions where it requires just one or two commands.

Take for example function definition bellow

compile()
{
  gcc "$1" -o "${1%%.*}" && printf "<<< Compiled successfully\n"
}

By placing it into your .bashrc file (and then running source ~/.bashrc or opening new terminal tab) , you can run this command from anywhere without having ~/bin added to your PATH variable, and provide a filename as command line argument like so

compile somecode.c

On a side note, you can edit that file just by calling gedit ~/.bashrc from command line

For those of you wondering what does "$1" and "${1%%.*}" , the "$1" refers to the first command line parameter ( in this case "somecode.c" ), as for "${1%%.*}" - that's parameter expansion , particularly the one that does suffix removal , and will throw away anything after the dot. In other words , that turns somecode.c into somecode . For more info, read bash manual page section about parameter expansion

Solution 3:

Before getting into this, I wanted to clarify that you're not creating new commands, you're creating scripts that can be run as executables. But they can behave in the same way as a command because you can run them from anywhere.

If you don't have a folder in your home folder called "bin", create one.

Now open a terminal window and type nano ~/.bashrc.

This will open an editor. At the bottom of this file, add a new line that says PATH="$HOME/bin:$PATH".

Now save the file by pressing CTRL+O (the letter o, not zero), then press ENTER. Exit the editor with CTRL+X. Logout and back in for changes to take effect, or restart your computer if that doesn't work.

Now any scripts you create in your bin folder will be accessible anywhere in a terminal window.

You can create these scripts with either nano, or gedit (the Ubuntu graphical text editor) or any text editor you wish. Just remember to save your script in the ~/bin folder and make your file executable. There 2 ways to do this:

GUI : Right click the file and go to the permissions tab, and check the box "Allow executing as a program".

TERMINAL : chmod +x /location/of/your/file