11

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?

  • I tried out all of these techniques and they all worked! Thanks everyone! – agentbanks217 Feb 10 '11 at 21:57
  • Try also the solution in my edit, as I realised that you really intended to have 'sudo shutdown' as an alias a little too late. – Costi Ciudatu Feb 10 '11 at 22:11
19

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.

  • 2
    Thanks, underrated answer; I've been using alias cd="cd " with single-letter aliases alias X="<directory-with-long-name>" to jump from place to place more easily. – Luke Davis Jun 7 '17 at 18:50
  • Agree, this is actually super useful. I mis-type git commands, this solves that for me: alias gti="git " and alias lgo="log" – BananaNeil Oct 30 '17 at 1:11
4

Use a function:

foo() { if [ "$1" = "bar" ]; then echo "doing something..."; fi; }
  • You probably want an else case so that the original command runs if the second parameter isn't bar: foo() { if [ "$1" = "bar" ]; then echo "doing something..."; else command foo "$@"; fi; } – davidg Mar 20 '18 at 22:38
2

To quote from: Bash: Spaces in alias name

The Bash documentation states "For almost every purpose, shell functions are preferred over aliases." Here is a shell function that replaces ls and causes output to be piped to more if the argument consists of (only) -la.

ls() {
    if [[ $@ == "-la" ]]; then
        command ls -la | more
    else
        command ls "$@"
    fi
}

As a one-liner:

ls() { if [[ $@ == "-la" ]]; then command ls -la | more; else command ls "$@"; fi; }

Automatically pipe output:

ls -la
1

You can use a dash as a work around: alias sudo-shutdown="your command"

1

I prefer to just put a executable file in /usr/local/bin (be sure that is in your path) that then contains the commands I wish to run. The name of the script would be the command that comes up. be sure to chmod +x /usr/local/bin/your_script.

So the script could be called 'shutdown'

The contents of the file shutdown would be:

#!/bin/sh
sudo shutdown

That opens a whole new world. You could then have a couple commands or even a script:

#!/usr/bin/env python
print "hi"
# more stuff. ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.