Make a Bash alias that takes a parameter?

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

    Refining the answer above, you can get 1-line syntax like you can for aliases, which is more convenient for ad-hoc definitions in a shell or .bashrc files:

    bash$ myfunction() { mv "$1" "$1.bak" && cp -i "$2" "$1"; }
    
    bash$ myfunction original.conf my.conf
    

    Don't forget the semi-colon before the closing right-bracket. Similarly, for the actual question:

    csh% alias junk="mv \\!* ~/.Trash"
    
    bash$ junk() { mv "$@" ~/.Trash/; }
    

    Or:

    bash$ junk() { for item in "$@" ; do echo "Trashing: $item" ; mv "$item" ~/.Trash/; done; }
    

提交回复
热议问题