1

I am trying to use the solution of using sudo on my existing aliases as covered in many existing answers already and i am so confused as to why it is not working

alias sudo='sudo '

I keep all my aliases in .bash_aliases. Inside .bash_aliases I have this

function _test {
    echo 'test!'
}

alias test='_test'

I reload the .bashrc each time; source .bashrc but when I run sudo test I always get

sudo: _test: command not found

The only strange thing that happens is that I get the following on reload and not on a new terminal

dircolors: /home/MYHOME/.dircolors: No such file or directory

but i feel this is a red herring.

9
  • 3
    sudo runs in a clean-ish environment. You can't execute user shell functions like this. The only reason the alias works is because it's expanded before sudo is called. Commented Feb 2, 2021 at 16:54
  • 3
    alias sudo='sudo ' doesn't do anything interesting. Nor does your function alias; you could simply define test() { echo 'test!'; } in the first place, no alias needed. Commented Feb 2, 2021 at 17:02
  • 1
    @myol chepner's comment does indeed stand. sudo uses execve() to directly invoke the executable that is to be its child process; that's an OS-level syscall. aliases are just an interactive shell construct; they don't work at any other layer, including noninteractive shell scripts (and it's bad form to rely on them at all; while whether they can be exported across a privilege boundary depends on the details of how sudo is configured and it's much better from a security perspective if they're disallowed, exported functions can sometimes work through sudo, whereas aliases never will). Commented Feb 2, 2021 at 17:08
  • 1
    Anyhow -- I'd argue this isn't on-topic here at all; it's a end-user question, not a software-development-specific one. Unix & Linux or Super User are better suited. Commented Feb 2, 2021 at 17:10
  • 1
    @myol You cannot directly sudo a function, an alias or a builtin, only an executable file that is on your disk. The indirect way of doing this is through sudo bash -c .... All this looks like an XY problem to me, though Commented Feb 2, 2021 at 20:14

2 Answers 2

1

As l0b0 says, aliases cannot be used in this way in bash.

However, you can pass a function through (and really, there's basically never a good reason to use an alias instead of sticking to functions alone).

_test() {
    echo 'test!'
}

sudo_test() {
  sudo bash -c "$(declare -f _test)"'; _test "$@"' sudo_test "$@"
}

...will define a command sudo_test that runs the function _test via sudo. (If your real-world version of this function calls other functions or requires access to shell variables, add those other functions to the declare -f command line, and/or add a declare -p inside the same command substitution to generate a textual description of variables your function needs).

Sign up to request clarification or add additional context in comments.

Comments

0

To run an alias like alias_name it must be exactly the first word in the command, so sudo alias_name will never work. Ditto 'alias_name', \alias_name and other things which eventually expand to the alias name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.