James Ashwood
James Ashwood

Reputation: 579

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.

Upvotes: 0

Views: 123

Answers (4)

James Ashwood
James Ashwood

Reputation: 579

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.

Upvotes: 0

Cristian Ramon-Cortes
Cristian Ramon-Cortes

Reputation: 1888

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.

Upvotes: 1

user1934428
user1934428

Reputation: 22225

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.

Upvotes: 1

Tony Stark
Tony Stark

Reputation: 2468

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

alias la="la.sh"

Upvotes: 1

Related Questions