Monday, January 31, 2022

[SOLVED] How can I create a command in linux, is alias the only way?

Issue

I come here, just for context, based on this question. How to set bash aliases for docker containers in Dockerfile?

I know I could create an alias for a certain command and inject them to the bash profile of the users.

For example:

alias kc=/opt/kafka/bin/kc.sh

I could also add the directory to the path (probably this is what I'll do).

But I wanted to know is there a way I could create this as a kind of command? Is alias the only way to do this, and is there a way I could create the alias for all users not depending on the bash they use?

For example if I would want to create a command something like my_command is there a way to do this?

Probably this question is showing lots of lack of knowledge of the actual operating system but I just wanted to know what options I would have, and my google searches always show the same results so I thought I asked here


Solution

You have the command already: /opt/kafka/bin/kc.sh.

I understand that you want to make it available to your users as a simple kc.sh or kc, without the directory prefix. The solution is to either put that directory into the PATH variable, typically done in /etc/profile; or put the command respectively a (symbolic) link to it into a directory which is already in the PATH, like /bin/, /usr/bin or /usr/local/bin.

Putting the directory into the PATH is probably the better solution, also because there are probably more useful commands to be found. If you don't like kc.sh but want a simple kc you can still make a link in the same directory, e.g. by performing cd /opt/kafka/binand then ln -s kc.sh kc.



Answered By - Peter - Reinstate Monica
Answer Checked By - David Goodson (WPSolving Volunteer)