2

Let's say I want to create an alias called ss for the command sudo -s. But ss is already an existing command, so I want to create another alias called sst for the command ss.

If using just the normal command names, this is not possible, since aliases:

  1. Are not set in a way that respects the order, and
  2. Reference other aliases, instead of only referencing commands

So if I try the following:

alias sst='ss'
alias ss='sudo -s'

Running the command sst results in running sudo -s, which is not my intention.

How can this be done?

Artur Meinild
  • 26,018
  • Why not s as you seem to be adverse to typing an extra character to have an alias not conflict with existing command. It is only single letter and is not in use. –  Feb 26 '21 at 16:04
  • I could do s, but this was more for example sake. Also, let's say I wanted another sudo alias (sudo -v for example). Then maybe it's easier to remember it's ss and sv. – Artur Meinild Feb 26 '21 at 16:07
  • I tried to search for an answer to this, but didn't find anything. Thanks for pointing out there was an existing answer (it was difficult to know exactly what to search for). – Artur Meinild Feb 26 '21 at 16:10
  • 2
    command ss or \ss as the alias definition would be enough – muru Feb 26 '21 at 16:11

1 Answers1

2

For the above to work, you need to reference the absolute path of the command you want to run.

The can be done with the following:

alias sst='/usr/bin/ss'
alias ss='sudo -s

However, one can't always be sure ss is at that location, so a more solid approach would be:

alias sst='$(which ss)'
alias ss='sudo -s

Now the above works as expected, where ss runs sudo -s and sst runs the command ss.

Artur Meinild
  • 26,018