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

2019-08-03 10:42发布

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条回答
Anthone
2楼-- · 2019-08-03 10:51

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 "$@" 
}
查看更多
登录 后发表回答