How to define/undefine an alias/function in bash
So many interesting things to learn here.
I knew about aliases and use them all the time. I had forgotten how to undefine them though, so ...
alias
Here is how to define one:
alias someaction="some sort of action or something here"
You use it like you'd expect. At the command-line, just enter someaction
and it will do it's thing! Very nice.
Once you've defined it and you don't want or need it anymore, remove it by:
unalias someaction
It's as easy as that. If you want to know all of the aliases you've got defined, what you need to do is type
- alias
- declare -f alias
- declare -a
at the command-line and it will list all of the currently defined aliases.
shell functions
Now, one thing I hadn't ever really thought about is that you can define functions (called shell functions) in the terminal! Here is an example:
export MY_BIN_DIR="/Users/$(whoami)/.mike/my_setup/bin"
#### by arunkumar AND Evan Langlois (2 entries)
#### from: https://stackoverflow.com/questions/7131670/make-a-bash-alias-that-takes-a-parameter
#### -------------------
#### will execute the first parameter and pass any other parameters beyond
#### that into the function (or, at least, that is the hope)
mybin() {
${MY_BIN_DIR}/$@
}
You can find out all of the different shell functions you have in a couple of different ways:
- use the declare command
- use the -F option (i.e. -- declare -F) to list all of the names of the shell functions that are defined
- use the -f option (i.e. -- declare -f) to list the source and names of all of the shell functions that are defined
- for searching for (I suppose - didn't see this listed, but tried it and it works) a specific name to see if a shell function of that names exists, then, use
- declare -F func_name and if it exists, it will return it and if not, it will return nothing
- compgen -A function - will display the name of the functions
- set | grep " ()"
- for listing the source of a shell function there are (again) several ways
- declare -f func_name* - will display the source of the function, if** it exists
- type func_name
Here is an interesting shell function that will list all of the defined shell functions and aliases. Very neat:
function functionaliaslist() {
echo
echo -e "\033[1;4;32m""Functions:""\033[0;34m"
compgen -A function
echo
echo -e "\033[1;4;32m""Aliases:""\033[0;34m"
compgen -A alias
echo
echo -e "\033[0m"
}
awesome links
- How do I delete/remove a shell function
- How do I list the functions defined in my shell?
- Programmable Completion Builtins - this has a lot of interesting information for
compgen
. I should probably look over this later. - Understand
compgen
builtin command