Archive for April, 2013

Alias `su 0` to `su -`

Monday, April 22nd, 2013

You can only alias a command, not a command + argument(s). The process of automatically fixing oft’ mistyped arguments involves 2 steps:
1. Alias the command to a shell script
2. Write shell script to replace the erroneous argument with the real one.

For step one, create the following alias:
alias su="~/bin/su_improved.sh $@"

Although I found many sources claiming you can’t redirect all a commands arguments to another command, $@ does exactly that. I mention this in case my system happens to be the exception rather than the rule.

The script, ~/bin/su_improved.sh:

#!/bin/bash
# Alias 'su 0' to 'su -'
 
# Created by KJ Coop on Sun Apr 21 23:24:28 PDT 2013
# Yoinked from http://kjcoop.org/2013/04/22/alias-su-0-to-su/
 
if [ "$1" == "0" ]; then
    # I can't `su - @#` because it expands into `su - 0...` so I'm just
    # assuming there's a max of 2 arguments: su [dash] [username]
    su - "$2"
else
    su $@
fi
 
exit 0;