How to alias a command with spaces? (Or a shell fu

2019-08-03 10:46发布

问题:

This question already has an answer here:

  • Can I alias a subcommand? (shortening the output of `docker ps`) 1 answer
  • How to write an alias for “two” words [duplicate] 2 answers

All the documentation I've looked at seems to indicate that in both aliases and shell functions, the name cannot contain spaces.

What I'm trying to do is make it more difficult for other admins (as root) to run a command against our Pass implementation (doc here : https://www.passwordstore.org/). It would still be possible, but I was hoping to turn a command like "pass rm $anyValueTheyInput" and alias that to, say "echo 'You can't do that'". Of course they're admins and they can change their aliases, but it would hopefully prevent accidental removal of passwords.

Is this possible in BASH? These will all be on RHEL or Centos boxes.

回答1:

Aliasing whole commands complete with arguments is not possible in bash (if it's even possible in any UNIX shell at all).

What you can do is create a pass function that catches all the undesirable argument packs and forwards all other argument packs to command pass:

pass()
{
    if [ rm = "$1" ]; then 
        >&2 printf '%s\n' "You can't do that"
        return 1
    fi
    #more checks... ?
    #...
    #forward the sanitized argument pack into the actual pass binary/script
    command pass "$@" 
}