Make a Bash alias that takes a parameter?

后端 未结 20 1974
长发绾君心
长发绾君心 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:17

    For taking parameters, you should use functions!

    However $@ get interpreted when creating the alias instead of during the execution of the alias and escaping the $ doesn’t work either. How do I solve this problem?

    You need to use shell function instead of an alias to get rid of this problem. You can define foo as follows:

    function foo() { /path/to/command "$@" ;}
    

    OR

    foo() { /path/to/command "$@" ;}
    

    Finally, call your foo() using the following syntax:

    foo arg1 arg2 argN
    

    Make sure you add your foo() to ~/.bash_profile or ~/.zshrc file.

    In your case, this will work

    function trash() { mv $@ ~/.Trash; }
    

提交回复
热议问题