Make a Bash alias that takes a parameter?

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

    All you have to do is make a function inside an alias:

    $ alias mkcd='_mkcd(){ mkdir "$1"; cd "$1";}; _mkcd'
                 ^        *      ^  ^     ^  ^         ^
    

    You must put double quotes around "$1" because single quotes will not work. This is because clashing the quotes at the places marked with arrows confuses the system. Also, a space at the place marked with a star is needed for the function.

    0 讨论(0)
  • 2020-11-21 07:24

    You don't have to do anything, alias does this automatically.

    For example, if i want to make git pull origin master parameterized, i can simply create an alias as follows:

    alias gpull = 'git pull origin '
    

    and when actually calling it, you can pass 'master' (the branch name) as a parameter, like this:

    gpull master
    //or any other branch
    gpull mybranch
    
    0 讨论(0)
提交回复
热议问题