I have a function in my ~/.zshrc
:
findPort() {
lsof -t -i :$1
}
The usual invocation is findPort 3306
.
I want to run it with elevated privileges. But I get "command not found".
➜ git 🍔 sudo findPort 3306
sudo: findPort: command not found
I presume the reason is that the root user either runs as a non-interactive shell (thus does not refer to a .zshrc), or refers to a different .zshrc
.
I have seen similar questions regarding alias
, but no question regarding user-defined functions. The answers for this problem regarding alias
involves adding an alias to ~/.zshrc
:
alias sudo='nocorrect sudo '
Or perhaps:
alias sudo='sudo '
I have tried both of these solutions, and the problem still exists (yes I've relaunched the shell).
I have also tried running sudo chsh
to ensure that my root shell runs under zsh
. None of these solutions removes the "command not found" problem.
Is there a way to run my user-defined functions under sudo?