Make a Bash alias that takes a parameter?

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

    Bash alias does not directly accept parameters. You will have to create a function.

    alias does not accept parameters but a function can be called just like an alias. For example:

    myfunction() {
        #do things with parameters like $1 such as
        mv "$1" "$1.bak"
        cp "$2" "$1"
    }
    
    
    myfunction old.conf new.conf #calls `myfunction`
    

    By the way, Bash functions defined in your .bashrc and other files are available as commands within your shell. So for instance you can call the earlier function like this

    $ myfunction original.conf my.conf
    

提交回复
热议问题