Make a Bash alias that takes a parameter?
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
Alias with input. Unix
csh
records your command in its history list prior to expanding aliases, so you can use history expansion to access arguments to the alias when it is used.
% alias myGrep grep -rn \!:1 . --color
When you use myGrep foo
, that two-word command is recorded in history, then it is expanded to grep -rn !:1 . --color
. In that command, !:1
refers to the first argument of the previous command (myGrep foo
), resulting in grep -rn foo . --color
, which is actually executed.
Alias with variable in bash
I'd create a function for that, rather than alias, and then exported it, like this:
function tail_ls { ls -l "$1" | tail; }
export -f tail_ls
Note -f
switch to export
: it tells it that you are exporting a function. Put this in your .bashrc
and you are good to go.
User Input to Bash Alias?
An alias cannot accept parameters, so you need to create a function:
acp ()
{
git add -A;git commit -m "$1";git push
}
as always, store it in ~/.bashrc
and source it with source ~/.bashrc
.
Or better (good hint, binfalse) to avoid performing a command if the previous was not successful, add &&
in between them:
acp ()
{
git add -A && git commit -m "$1" && git push
}
Execute it with
acp "your comment"
It is important that you use double quotes, otherwise it will just get the 1st parameter.
Passing argument to alias in bash
An alias will expand to the string it represents. Anything after the alias will appear after its expansion without needing to be or able to be passed as explicit arguments (e.g. $1
).
$ alias foo='/path/to/bar'
$ foo some args
will get expanded to
$ /path/to/bar some args
If you want to use explicit arguments, you'll need to use a function
$ foo () { /path/to/bar "$@" fixed args; }
$ foo abc 123
will be executed as if you had done
$ /path/to/bar abc 123 fixed args
To undefine an alias:
unalias foo
To undefine a function:
unset -f foo
To see the type and definition (for each defined alias, keyword, function, builtin or executable file):
type -a foo
Or type only (for the highest precedence occurrence):
type -t foo
How to pass command line arguments to a shell alias?
You found the way: create a function instead of an alias. The C shell has a mechanism for doing arguments to aliases, but bash and the Korn shell don't, because the function mechanism is more flexible and offers the same capability.
Linux shell: How to give parameter to alias of find command?
Use function:
myfind() { find . ! -path "./build" -name "$1"|xargs grep "$2"; }
Related Topics
In Shellscript Assign Variable Based on Curl Output
The Button.Connect Syntax in Genie
Does Routing Affect a Socket with a Bound Source Address
Reading Microphone Data by Polling Using Alsa [Or V4L2]
Convert a Base64 Ldif File to Plaintext (For Import)
Aosp Build Error: Unrecognized Module Type "Hidl_Package_Root"
Golang Math Can Not Finished with My Code, But Python Is Ok
Fork, Execlp and Kill. Zombie Process
Invalid Choice: 'Kernel_Add_Dts' in Yocto Build
How to Run a Bash Function() in a Remote Host? in Ubuntu
Sort a Find Command to Respect a Custom Order in Unix
Omnibus or Source - Can't Decide Which One to Use for Gitllab Backup/Restore
Redirecting Output of a C Program to Another C Program with a Bash Script Under Linux
Run a Script When a New Veth Interface Is Added