Make a Bash alias that takes a parameter?

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

    If you're looking for a generic way to apply all params to a function, not just one or two or some other hardcoded amount, you can do that this way:

    #!/usr/bin/env bash
    
    # you would want to `source` this file, maybe in your .bash_profile?
    function runjar_fn(){
        java -jar myjar.jar "$@";
    }
    
    alias runjar=runjar_fn;
    

    So in the example above, i pass all parameters from when i run runjar to the alias.

    For example, if i did runjar hi there it would end up actually running java -jar myjar.jar hi there. If i did runjar one two three it would run java -jar myjar.jar one two three.

    I like this $@ - based solution because it works with any number of params.

提交回复
热议问题