Search code examples
linuxbashcommand-linegnome-terminalmanjaro

How do I make a command in linux using a bash script?


I am trying to make a command for the terminal. I have a bash script prepared (la.sh) and I want to just be able to type la to run it. How am I able to get the code so that I can just type la?

I have tried putting it in the /bin folder however had no luck.

What can I do to fix this?

I am using the latest version of Manjaro Gnome.

Thanks a lot!!!

BTW, the script was literally just ls.

It was just a practice script.


Solution

  • Lets consider that your script is stored under /some/path/la.sh. In my opinion, you have several solutions to accomplish your goal:

    Option 1:

    Add the script to your user's path so you can directly call it.

    echo "export PATH=$PATH:/some/path/" >> ~/.bashrc
    

    Then you will be able to use in your terminal:

    $ la.sh
    

    Using this option you can call la.sh with any parameters if needed. If the requirement is to call simply la you can also rename the script or create a softlink:

    mv /some/path/la.sh /some/path/la
    

    or

    ln -s /some/path/la.sh /some/path/la
    

    Option 2:

    Create an alias for the script.

    echo "alias la='/some/path/la.sh'" >> ~.bashrc
    

    Then you will be able to use in your terminal:

    $ la
    

    However, using this option you will not be able to pass arguments to your script (executing something similar to la param1 param2) unless you define a more complex alias (an alias using a function in the .bashrc, but I think this is out of the scope of the question).

    IMPORTANT NOTE: Remember to reload the environment in your terminal (source .bashrc) or to close and open again the terminal EVERY TIME you make modifications to the .bashrc file. Otherwise, you will not be able to see any changes.