Is there a way to alias a multi-worded command in Unix?

I am trying to alias the command sudo shutdown but in my .profile I can't just do alias "sudo shutdown"="my command". Is there another way to alias multi-worded commands in Unix?


Solution 1:

The alias name is on the left hand side, so you can have any multiworded command in the right:

alias my_alias="multi worded command"

EDIT: However, if you really want to have aliases names of multiple words -- in your example, if you really want "sudo shutdown" to be the alias, you can work around this by using the following alias feature (in the bash manual):

A trailing space in  value causes the next word to be checked for
alias substitution when the alias is expanded.

So, if you still want to use sudo with other commands but not with shutdown and if you can afford to alias shutdown to something else, you can have:

alias sudo='sudo '
alias shutdown="echo nope"

Now, sudo anything_else will work as expected (assuming you don't have an alias for anything_else, but if you have it wouldn't have worked before either); but sudo shutdown will be equivalent to sudo echo nope and it'll print that string.

Solution 2:

Use a function:

foo() { if [ "$1" = "bar" ]; then echo "doing something..."; fi; }