Make a Bash alias that takes a parameter?

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

    Bash alias absolutely does accept parameters. I just added an alias to create a new react app which accepts the app name as a parameter. Here's my process:

    Open the bash_profile for editing in nano

    nano /.bash_profile
    

    Add your aliases, one per line:

    alias gita='git add .'
    alias gitc='git commit -m "$@"'
    alias gitpom='git push origin master'
    alias creact='npx create-react-app "$@"'
    

    note: the "$@" accepts parameters passed in like "creact my-new-app"

    Save and exit nano editor

    ctrl+o to to write (hit enter); ctrl+x to exit

    Tell terminal to use the new aliases in .bash_profile

    source /.bash_profile
    

    That's it! You can now use your new aliases

提交回复
热议问题