Make a Bash alias that takes a parameter?

后端 未结 20 2317
长发绾君心
长发绾君心 2020-11-21 06:53

I used to use CShell (csh), which lets you make an alias that takes a parameter. The notation was something like

alias junk=\"mv \\\\!* ~/.Trash\"

20条回答
  •  深忆病人
    2020-11-21 07:09

    Functions are indeed almost always the answer as already amply contributed and confirmed by this quote from the man page: "For almost every purpose, aliases are superseded by shell functions."

    For completeness and because this can be useful (marginally more lightweight syntax) it could be noted that when the parameter(s) follow the alias, they can still be used (although this wouldn't address the OP's requirement). This is probably easiest to demonstrate with an example:

    alias ssh_disc='ssh -O stop'
    

    allows me to type smth like ssh_disc myhost, which gets expanded as expected as: ssh -O stop myhost

    This can be useful for commands which take complex arguments (my memory isn't what it use t be anymore...)

提交回复
热议问题