-1

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

Biffen
  • 6,249
  • 6
  • 28
  • 36
Miguel Costa
  • 627
  • 1
  • 12
  • 30
  • 2
    You can define shell functions, you can write shell scripts, you can write a program in practically any language. – Barmar Apr 01 '21 at 11:31
  • 1
    Are you really asking how to make the new command available to all users on the system? Put it in a directory that's in everyone's `$PATH`. Often `/usr/local/bin` is used for this. – Barmar Apr 01 '21 at 11:32
  • 1
    [unix.se] would probably be a better place to ask about things like this. – Barmar Apr 01 '21 at 11:33
  • I can create a script and put it in the path, I was just wondering if there was another way. I think alias is more similar to what I want to do but I wanted to know if there was a simpler way to do this – Miguel Costa Apr 01 '21 at 11:36
  • Alias should generally only be used for very simple commands. It's just a simple substitution, it can't process arguments. – Barmar Apr 01 '21 at 11:38
  • Where do people get their information? Since before 1996, the advice has been "Don't use aliases". And here in 2021 there is a persistent belief not only that aliases are reasonable, but apparently believed to be the only option! Don't use aliases! Write shell functions. Write shell scripts or build executables and put them in your PATH. Put `$HOME/bin` or similar in your PATH. aliases are a road down which you do not wish to go. – William Pursell Apr 01 '21 at 12:33
  • depending what you use them for I have no problems with alias, I guess for all users indeed the path and some function/executable or whatever seems to be the best pattern. still as a user sometimes, just for the laziness alias can be quite useful for the commands you execute all the time and what somehow to shorten them – Miguel Costa Apr 01 '21 at 14:41

1 Answers1

2

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.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
  • thanks for the feedback, I guess indeed the simpler is just to append it to the path since it does most of what is required anyway. I can still create an alias but indeed I think just putting this into the path is the simpler – Miguel Costa Apr 01 '21 at 12:00
  • 1
    @MiguelCosta Not sure why you are so focused on aliases. One issue with them is that they are shell specific. If you really want system-wide alternative names for commands I'd recommend symbolic links. – Peter - Reinstate Monica Apr 01 '21 at 12:10
  • not focused on the alias, it's just that most of the examples I found was using alias... but understood if having it in the path is the best and easier way that's what I'll do. probably will not create the symbolic link – Miguel Costa Apr 01 '21 at 14:39