0

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.

4
  • 4
    If you want to call it la, why did you call it la.sh? Commented Feb 21, 2020 at 11:12
  • 2
    Try adding an alias in the .alias or .bashrc files of your user. Also, if you are just simply calling the script you can rename it to la and add it to your user path (also in the .bashrc file) Commented Feb 21, 2020 at 11:13
  • 1
    Please show the script - you'll receive better advice. Commented Feb 21, 2020 at 12:28
  • 1
    The short answer is that if you want to type la to run a script, then write the script and save it with the name la in a directory on your PATH and make sure it is executable with chmod +x path/to/where/you/stored/la Commented Feb 21, 2020 at 15:53

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@CristianRamonCortes : I don't think option 1 fulfills the requirement, because after the mv, the OP can't use the original name la.sh again. If he had wanted the command to be available only as la, he would have created it under this name in the first place!
@user1934428 Maybe, maybe not, he didn't state that. I updated my answer using a softlink though.
He didn't say it, but I think it is logical. Who would create a program under one name, if he wants to invoke it (only) under a different name?
1

The file la.sh must be placed in your path. Then you can create an alias for it.

alias la="la.sh"

Comments

1

If you want to have a command be available under two different names (la.sh and la in your case), I recommend against using an alias: An alias defined in your .bashrc is only available in an interactive bash; If you run, say, a non-bash interactive shell, or writing a bash script, you can't use it.

The IMO most general way is to create a link. Since you said that you have already placed la.sh into bin, you can create the link in the same directory, i.e.

ln /bin/la /bin/la.sh # creates a hard link

or

ln -s /bin/la /bin/la.sh # creates a symbolic link

In your case, either one is fine. If you want to find out more about the differences between hard and symbolic link, look for instance here.

Comments

0

It worked with a mixture of everybody's answers.

All I had to do was go into the directory that la.sh was in. Rename it to just la as a text file. Run chmod 777 la to turn it into executable to anybody. Add it to my path by using the command export PATH=$PATH:~/Directory/It/Was/In/

Thank you to all who contributed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.