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.

myol
  • 8,857
  • 19
  • 82
  • 143
  • 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. – jordanm Feb 02 '21 at 16:54
  • I updated the question to match my actual issue, but perhaps your comment still stands – myol Feb 02 '21 at 16:58
  • 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. – chepner Feb 02 '21 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). – Charles Duffy Feb 02 '21 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.se] or [Super User](https://superuser.com/) are better suited. – Charles Duffy Feb 02 '21 at 17:10
  • @chepner It was a minimal reproduction, replace echo with a command that needs elevated user privileges. Regardless, if I simply use `test() {echo 'test!';}`, with sudo, nothing is output – myol Feb 02 '21 at 17:15
  • 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 – xhienne Feb 02 '21 at 20:14
  • 1
    BTW, `function funcname { ... }` is legacy ksh syntax. Bash is compatible with that aspect of ksh, but some other modern shells aren't; it's better to use POSIX sh function syntax -- `funcname() {` with no `function`. See also https://wiki.bash-hackers.org/scripting/obsolete, particularly the entry in the 3rd table (the 1st one covers syntax with no legitimate use outside code golf; the 3rd one, things only to use given a specific reason to prefer them, is the relevant one). – Charles Duffy Sep 17 '21 at 11:40
  • Tangentially, [Why would I create an alias which creates a function?](https://stackoverflow.com/questions/57239089/why-would-i-create-an-alias-which-creates-a-function) discusses a related antipattern which is somewhat relevant for this case, too. – tripleee Feb 18 '22 at 06:55

2 Answers2

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).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
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.

l0b0
  • 55,365
  • 30
  • 138
  • 223