Think of aliases as substituting a word with some other text in shell code before it is interpreted.
With a find
alias defined as:
alias find='function _find(){find / -name "$1" 2>&1 | grep -v "Permission denied"}'
When you type
find
at the prompt, that gets substituted with:
function _find(){find / -name "$1" 2>&1 | grep -v "Permission denied"}
If the shell was zsh
, that code would define a _find
function whose body is find / -name "$1" 2>&1 | grep -v "Permission denied"
. In other shells including bash
, you would get a syntax error either because function f()
is not the proper variable declaration syntax or (like in bash
or yash
) because that {find...
would be treated as a simple command and bash
and yash
happen to be the only shells that don't support simple commands as function body.
Here, you want do define a function, not an alias, and since that function would have a different API from the find
command's, you'd want to call it my another name like:
myfind() {
find / -name "$@" 2>&1 | grep -v "Permission denied"
}
In any case, whether that's an alias or a function, as both are internal features of the shell, you won't be able to use them from sudo
. For sudo
to be able to call it, you'd have to make it a real command like a script:
#! /bin/sh -
find / -name "$@" 2>&1 | grep -v "Permission denied"
You'd need make that script executable and store it in a directory that is in sudo
's secure_path
if defined.
.bashrc
or.bash_aliases
. Call_find
from the command line. It is not a good idea to always search/
. – markgraf Oct 09 '19 at 14:57alias somecommand='command somecommand --with-options whatever'
. – markgraf Oct 09 '19 at 15:00sudo
, it would need to be in a script anyway. – ChumiestBucket Oct 09 '19 at 18:35