How to create a bash alias with arguments

Here is a way to create a bash alias with arguments.

myecho(){ echo [email protected]; }
alias mycommand='myecho'

We have created:

  • A function myecho, which will echo all arguments.
  • An alias mycommand for calling that function.

Example #1. a bash alias with 1 argument

[email protected]:~/sample-git$ myecho(){ echo [email protected]; }
[email protected]:~/sample-git$ alias mycommand='myecho'
[email protected]:~/sample-git$ 
[email protected]:~/sample-git$ mycommand TESTING
TESTING
[email protected]:~/sample-git$ 

Example #2. a bash alias with multiple arguments

[email protected]:~/sample-git$ mycommand arg1 arg2 arg3
arg1 arg2 arg3
[email protected]:~/sample-git$ 

Parameters (arguments) passed on to the function can be accessed via standard variables, such as $1, $2, $3, etc.

To make your alias permanently available, you have to add it in your ~/.bashrc or ~/.bash_profile file